function BundleClassTest::testEntitySubclass

Same name in other branches
  1. 10 core/tests/Drupal/KernelTests/Core/Entity/BundleClassTest.php \Drupal\KernelTests\Core\Entity\BundleClassTest::testEntitySubclass()
  2. 11.x core/tests/Drupal/KernelTests/Core/Entity/BundleClassTest.php \Drupal\KernelTests\Core\Entity\BundleClassTest::testEntitySubclass()

Tests making use of a custom bundle class.

File

core/tests/Drupal/KernelTests/Core/Entity/BundleClassTest.php, line 45

Class

BundleClassTest
Tests entity bundle classes.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testEntitySubclass() {
    entity_test_create_bundle('bundle_class');
    // Ensure we start life with empty counters.
    $this->assertEquals(0, EntityTestBundleClass::$preCreateCount);
    $this->assertEquals(0, EntityTestBundleClass::$preDeleteCount);
    $this->assertEquals(0, EntityTestBundleClass::$postDeleteCount);
    $this->assertEquals(0, EntityTestBundleClass::$postLoadCount);
    $this->assertCount(0, EntityTestBundleClass::$postLoadEntitiesCount);
    // Verify statically created entity with bundle class returns correct class.
    $entity = EntityTestBundleClass::create();
    $this->assertInstanceOf(EntityTestBundleClass::class, $entity);
    // Check that both preCreate() and postCreate() were called once.
    $this->assertEquals(1, EntityTestBundleClass::$preCreateCount);
    $this->assertEquals(1, $entity->postCreateCount);
    // Verify that none of the other methods have been invoked.
    $this->assertEquals(0, EntityTestBundleClass::$preDeleteCount);
    $this->assertEquals(0, EntityTestBundleClass::$postDeleteCount);
    $this->assertEquals(0, EntityTestBundleClass::$postLoadCount);
    $this->assertCount(0, EntityTestBundleClass::$postLoadEntitiesCount);
    // Verify statically created entity with bundle class returns correct
    // bundle.
    $entity = EntityTestBundleClass::create([
        'type' => 'custom',
    ]);
    $this->assertInstanceOf(EntityTestBundleClass::class, $entity);
    $this->assertEquals('bundle_class', $entity->bundle());
    // We should have seen preCreate() a 2nd time.
    $this->assertEquals(2, EntityTestBundleClass::$preCreateCount);
    // postCreate() is specific to each entity instance, so still 1.
    $this->assertEquals(1, $entity->postCreateCount);
    // Verify that none of the other methods have been invoked.
    $this->assertEquals(0, EntityTestBundleClass::$preDeleteCount);
    $this->assertEquals(0, EntityTestBundleClass::$postDeleteCount);
    $this->assertEquals(0, EntityTestBundleClass::$postLoadCount);
    $this->assertCount(0, EntityTestBundleClass::$postLoadEntitiesCount);
    // Verify that the entity storage creates the entity using the proper class.
    $entity = $this->storage
        ->create([
        'type' => 'bundle_class',
    ]);
    $this->assertInstanceOf(EntityTestBundleClass::class, $entity);
    // We should have seen preCreate() a 3rd time.
    $this->assertEquals(3, EntityTestBundleClass::$preCreateCount);
    $this->assertEquals(1, $entity->postCreateCount);
    // Nothing else has been invoked.
    $this->assertEquals(0, EntityTestBundleClass::$preDeleteCount);
    $this->assertEquals(0, EntityTestBundleClass::$postDeleteCount);
    $this->assertEquals(0, EntityTestBundleClass::$postLoadCount);
    $this->assertCount(0, EntityTestBundleClass::$postLoadEntitiesCount);
    // Verify that loading an entity returns the proper class.
    $entity->save();
    $id = $entity->id();
    $this->storage
        ->resetCache();
    $entity = $this->storage
        ->load($id);
    $this->assertInstanceOf(EntityTestBundleClass::class, $entity);
    // Loading an existing entity shouldn't call preCreate() nor postCreate().
    $this->assertEquals(3, EntityTestBundleClass::$preCreateCount);
    $this->assertEquals(0, $entity->postCreateCount);
    // Nothing has been deleted.
    $this->assertEquals(0, EntityTestBundleClass::$preDeleteCount);
    $this->assertEquals(0, EntityTestBundleClass::$postDeleteCount);
    // We should now have seen postLoad() called once.
    $this->assertEquals(1, EntityTestBundleClass::$postLoadCount);
    // It should have been invoked with a single entity.
    $this->assertCount(1, EntityTestBundleClass::$postLoadEntitiesCount);
    $this->assertEquals(1, EntityTestBundleClass::$postLoadEntitiesCount[0]);
    // Create additional entities to test invocations during loadMultiple().
    $entity_2 = $this->storage
        ->create([
        'type' => 'bundle_class',
    ]);
    $entity_2->save();
    $this->assertEquals(4, EntityTestBundleClass::$preCreateCount);
    $entity_3 = $this->storage
        ->create([
        'type' => 'bundle_class',
    ]);
    $entity_3->save();
    $this->assertEquals(5, EntityTestBundleClass::$preCreateCount);
    // Make another bundle that does not have a bundle subclass.
    entity_test_create_bundle('entity_test');
    $entity_test_1 = $this->storage
        ->create([
        'type' => 'entity_test',
    ]);
    $entity_test_1->save();
    // EntityTestBundleClass::preCreate() should not have been called.
    $this->assertEquals(5, EntityTestBundleClass::$preCreateCount);
    $entity_test_2 = $this->storage
        ->create([
        'type' => 'entity_test',
    ]);
    $entity_test_2->save();
    // EntityTestBundleClass::preCreate() should still not have been called.
    $this->assertEquals(5, EntityTestBundleClass::$preCreateCount);
    // Try calling loadMultiple().
    $entity_ids = [
        $entity->id(),
        $entity_2->id(),
        $entity_3->id(),
        $entity_test_1->id(),
        $entity_test_2->id(),
    ];
    $entities = $this->storage
        ->loadMultiple($entity_ids);
    // postLoad() should only have been called once more so far.
    $this->assertEquals(2, EntityTestBundleClass::$postLoadCount);
    $this->assertCount(2, EntityTestBundleClass::$postLoadEntitiesCount);
    // Only 3 of the 5 entities we just loaded use the bundle class. However,
    // one of them has already been loaded and we're getting the cached entity
    // without re-invoking postLoad(). So the custom postLoad() method should
    // only have been invoked with 2 entities.
    $this->assertEquals(2, EntityTestBundleClass::$postLoadEntitiesCount[1]);
    // Reset the storage cache and try loading again.
    $this->storage
        ->resetCache();
    $entities = $this->storage
        ->loadMultiple($entity_ids);
    $this->assertEquals(3, EntityTestBundleClass::$postLoadCount);
    $this->assertCount(3, EntityTestBundleClass::$postLoadEntitiesCount);
    // This time, all 3 bundle_class entities should be included.
    $this->assertEquals(3, EntityTestBundleClass::$postLoadEntitiesCount[2]);
    // Start deleting things and count delete-related method invocations.
    $entity_test_1->delete();
    // No entity using the bundle class has yet been deleted.
    $this->assertEquals(0, EntityTestBundleClass::$preDeleteCount);
    $this->assertEquals(0, EntityTestBundleClass::$postDeleteCount);
    $entity_test_2->delete();
    $this->assertEquals(0, EntityTestBundleClass::$preDeleteCount);
    $this->assertEquals(0, EntityTestBundleClass::$postDeleteCount);
    // Start deleting entities using the bundle class.
    $entity->delete();
    $this->assertEquals(1, EntityTestBundleClass::$preDeleteCount);
    $this->assertEquals(1, EntityTestBundleClass::$postDeleteCount);
    $entity_2->delete();
    $this->assertEquals(2, EntityTestBundleClass::$preDeleteCount);
    $this->assertEquals(2, EntityTestBundleClass::$postDeleteCount);
    // Verify that getEntityClass without bundle returns the default entity
    // class.
    $entity_class = $this->storage
        ->getEntityClass(NULL);
    $this->assertEquals(EntityTest::class, $entity_class);
    // Verify that getEntityClass with a bundle returns the proper class.
    $entity_class = $this->storage
        ->getEntityClass('bundle_class');
    $this->assertEquals(EntityTestBundleClass::class, $entity_class);
    // Verify that getEntityClass with a non-existing bundle returns the entity
    // class.
    $entity_class = $this->storage
        ->getEntityClass('custom');
    $this->assertEquals(EntityTest::class, $entity_class);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.