EntityLegacyTest.php 5.99 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\KernelTests\Core\Entity;

5 6
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
7
use Drupal\Core\Entity\EntityInterface;
8 9
use Drupal\entity_test\Entity\EntityTest;
use Drupal\entity_test\Entity\EntityTestMul;
10
use Drupal\entity_test\Entity\EntityTestRev;
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
use Drupal\KernelTests\KernelTestBase;

/**
 * Tests legacy entity functions.
 *
 * @group entity
 * @group legacy
 */
class EntityLegacyTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['entity_test', 'user'];

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    $this->installEntitySchema('entity_test');
    $this->installEntitySchema('entity_test_mul');
34
    $this->installEntitySchema('entity_test_rev');
35 36 37 38
  }

  /**
   * @expectedDeprecation entity_load_multiple() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use the entity type storage's loadMultiple() method. See https://www.drupal.org/node/2266845
39
   * @expectedDeprecation entity_load() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use the entity type storage's load() method. See https://www.drupal.org/node/2266845
40 41 42 43 44
   * @expectedDeprecation entity_get_bundles() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityTypeBundleInfoInterface::getBundleInfo() for a single bundle, or \Drupal\Core\Entity\EntityTypeBundleInfoInterface::getAllBundleInfo() for all bundles. See https://www.drupal.org/node/3051077
   * @expectedDeprecation entity_page_label() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use the entity's label() method. See https://www.drupal.org/node/2549923
   * @expectedDeprecation entity_revision_load() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use the entity type storage's loadRevision() method. See https://www.drupal.org/node/1818376
   * @expectedDeprecation entity_revision_delete() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use the entity type storage's deleteRevision() method. See https://www.drupal.org/node/1818376
   * @expectedDeprecation entity_load_unchanged() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use the entity type storage's loadUnchanged() method. See https://www.drupal.org/node/1935744
45 46 47 48 49 50 51 52 53 54 55 56 57
   */
  public function testEntityLegacyCode() {
    $this->assertCount(0, entity_load_multiple('entity_test'));
    $this->assertCount(0, entity_load_multiple('entity_test_mul'));

    EntityTest::create(['name' => 'published entity'])->save();
    $this->assertCount(1, entity_load_multiple('entity_test'));
    $this->assertCount(0, entity_load_multiple('entity_test_mul'));

    EntityTest::create(['name' => 'published entity'])->save();
    EntityTestMul::create(['name' => 'published entity'])->save();
    $this->assertCount(2, entity_load_multiple('entity_test'));
    $this->assertCount(1, entity_load_multiple('entity_test_mul'));
58 59 60

    $this->assertNull(entity_load('entity_test', 100));
    $this->assertInstanceOf(EntityInterface::class, entity_load('entity_test', 1));
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

    $this->assertEquals(['entity_test' => ['label' => 'Entity Test Bundle']], entity_get_bundles('entity_test'));
    $this->assertEquals(['entity_test' => ['label' => 'Entity Test Bundle']], entity_get_bundles()['entity_test']);

    $entity = EntityTestRev::create(['name' => 'revision test']);
    $entity->save();
    $this->assertEquals('revision test', entity_page_label($entity));
    $first_revision_id = $entity->getRevisionId();
    $entity->setNewRevision(TRUE);
    $entity->save();
    $first_revision = entity_revision_load($entity->getEntityTypeId(), $first_revision_id);
    $this->assertEquals($first_revision_id, $first_revision->getRevisionId());
    entity_revision_delete($entity->getEntityTypeId(), $first_revision_id);
    $this->assertNull(entity_revision_load($entity->getEntityTypeId(), $first_revision_id));

    $entity->setName('Different name');
    $entity = entity_load_unchanged($entity->getEntityTypeId(), $entity->id());
    $this->assertEquals('revision test', $entity->label());
79 80
  }

81 82 83 84 85 86 87 88 89 90 91 92 93
  /**
   * @expectedDeprecation entity_get_display() is deprecated in drupal:8.8.0. It will be removed before drupal:9.0.0. Use \Drupal::service('entity_display.repository')->getViewDisplay() instead. See https://www.drupal.org/node/2835616
   * @expectedDeprecation entity_get_form_display() is deprecated in drupal:8.8.0. It will be removed before drupal:9.0.0. Use \Drupal::service('entity_display.repository')->getFormDisplay() instead. See https://www.drupal.org/node/2835616
   */
  public function testLegacyDisplayFunctions() {
    $view_display = entity_get_display('entity_test', 'entity_test', 'default');
    $this->assertInstanceOf(EntityViewDisplayInterface::class, $view_display);
    $this->assertEquals('entity_test.entity_test.default', $view_display->id());
    $form_display = entity_get_form_display('entity_test', 'entity_test', 'default');
    $this->assertInstanceOf(EntityFormDisplayInterface::class, $form_display);
    $this->assertEquals('entity_test.entity_test.default', $form_display->id());
  }

94 95 96 97 98 99 100 101 102 103 104 105 106 107
  /**
   * @expectedDeprecation entity_view() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId())->view($entity, $view_mode, $langcode) instead. See https://www.drupal.org/node/3033656
   * @expectedDeprecation entity_view_multiple() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId())->viewMultiple($entities, $view_mode, $langcode) instead. See https://www.drupal.org/node/3033656
   */
  public function testEntityView() {
    $entity = EntityTest::create();
    $this->assertNotEmpty(entity_view($entity, 'default'));
    $entities = [
      EntityTest::create(),
      EntityTest::create(),
    ];
    $this->assertEquals(4, count(entity_view_multiple($entities, 'default')));
  }

108
}