class EntityStorageDeprecationTest

@coversDefaultClass \Drupal\Core\Entity\EntityStorageBase @group Entity @group legacy

@todo Remove this in Drupal 10.

Hierarchy

Expanded class hierarchy of EntityStorageDeprecationTest

See also

https://www.drupal.org/project/drupal/issues/3244802

File

core/tests/Drupal/Tests/Core/Entity/EntityStorageDeprecationTest.php, line 22

Namespace

Drupal\Tests\Core\Entity
View source
class EntityStorageDeprecationTest extends UnitTestCase {
    
    /**
     * The content entity database storage used in this test.
     *
     * @var \Drupal\Core\Entity\Sql\SqlContentEntityStorage|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $entityStorage;
    
    /**
     * The mocked entity type used in this test.
     *
     * @var \Drupal\Core\Entity\ContentEntityTypeInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $entityType;
    
    /**
     * An array of field definitions used for this test, keyed by field name.
     *
     * @var \Drupal\Core\Field\BaseFieldDefinition[]|\PHPUnit\Framework\MockObject\MockObject[]
     */
    protected $fieldDefinitions = [];
    
    /**
     * The mocked entity type manager used in this test.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $entityTypeManager;
    
    /**
     * The mocked entity type bundle info used in this test.
     *
     * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $entityTypeBundleInfo;
    
    /**
     * The mocked entity field manager used in this test.
     *
     * @var \Drupal\Core\Entity\EntityFieldManagerInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $entityFieldManager;
    
    /**
     * The entity type ID.
     *
     * @var string
     */
    protected $entityTypeId = 'entity_test';
    
    /**
     * The dependency injection container.
     *
     * @var \Symfony\Component\DependencyInjection\ContainerBuilder
     */
    protected $container;
    
    /**
     * The module handler.
     *
     * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $moduleHandler;
    
    /**
     * The cache backend to use.
     *
     * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $cache;
    
    /**
     * The language manager.
     *
     * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $languageManager;
    
    /**
     * The database connection to use.
     *
     * @var \Drupal\Core\Database\Connection|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $connection;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        $this->entityType = $this->createMock('Drupal\\Core\\Entity\\ContentEntityTypeInterface');
        $this->entityType
            ->expects($this->any())
            ->method('id')
            ->willReturn($this->entityTypeId);
        $this->entityType
            ->expects($this->any())
            ->method('getClass')
            ->willReturn('bogus_class');
        $this->container = new ContainerBuilder();
        \Drupal::setContainer($this->container);
        $this->entityTypeManager = $this->createMock(EntityTypeManager::class);
        $this->entityTypeBundleInfo = $this->createMock(EntityTypeBundleInfoInterface::class);
        $this->entityFieldManager = $this->createMock(EntityFieldManager::class);
        $this->moduleHandler = $this->createMock('Drupal\\Core\\Extension\\ModuleHandlerInterface');
        $this->cache = $this->createMock('Drupal\\Core\\Cache\\CacheBackendInterface');
        $this->languageManager = $this->createMock('Drupal\\Core\\Language\\LanguageManagerInterface');
        $this->languageManager
            ->expects($this->any())
            ->method('getDefaultLanguage')
            ->willReturn(new Language([
            'langcode' => 'en',
        ]));
        $this->connection = $this->getMockBuilder('Drupal\\Core\\Database\\Connection')
            ->disableOriginalConstructor()
            ->getMock();
        $this->container
            ->set('entity_type.manager', $this->entityTypeManager);
        $this->container
            ->set('entity_field.manager', $this->entityFieldManager);
    }
    
    /**
     * Sets up the content entity storage.
     */
    protected function setUpEntityStorage() {
        $this->connection = $this->getMockBuilder('Drupal\\Core\\Database\\Connection')
            ->disableOriginalConstructor()
            ->getMock();
        $this->entityTypeManager
            ->expects($this->any())
            ->method('getDefinition')
            ->willReturn($this->entityType);
        $this->entityTypeManager
            ->expects($this->any())
            ->method('getActiveDefinition')
            ->willReturn($this->entityType);
        $this->entityFieldManager
            ->expects($this->any())
            ->method('getFieldStorageDefinitions')
            ->willReturn($this->fieldDefinitions);
        $this->entityFieldManager
            ->expects($this->any())
            ->method('getActiveFieldStorageDefinitions')
            ->willReturn($this->fieldDefinitions);
        $this->entityStorage = new DeprecatedEntityStorage($this->entityType, $this->connection, $this->entityFieldManager, $this->cache, $this->languageManager, new MemoryCache(), $this->entityTypeBundleInfo, $this->entityTypeManager);
        $this->entityStorage
            ->setModuleHandler($this->moduleHandler);
    }
    
    /**
     * Tests the deprecation when accessing entityClass directly.
     *
     * @group legacy
     */
    public function testGetEntityClass() : void {
        $this->setUpEntityStorage();
        $this->expectDeprecation('Accessing the entityClass property directly is deprecated in drupal:9.3.0. Use ::getEntityClass() instead. See https://www.drupal.org/node/3191609');
        $entity_class = $this->entityStorage
            ->getCurrentEntityClass();
        $this->assertEquals('bogus_class', $entity_class);
    }
    
    /**
     * Tests the deprecation when setting entityClass directly.
     *
     * @group legacy
     */
    public function testSetEntityClass() : void {
        $this->setUpEntityStorage();
        $this->expectDeprecation('Setting the entityClass property directly is deprecated in drupal:9.3.0 and has no effect in drupal:10.0.0. See https://www.drupal.org/node/3191609');
        $this->entityStorage
            ->setEntityClass('entity_class');
        $this->assertEquals('entity_class', $this->entityStorage
            ->getEntityClass());
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
EntityStorageDeprecationTest::$cache protected property The cache backend to use.
EntityStorageDeprecationTest::$connection protected property The database connection to use.
EntityStorageDeprecationTest::$container protected property The dependency injection container.
EntityStorageDeprecationTest::$entityFieldManager protected property The mocked entity field manager used in this test.
EntityStorageDeprecationTest::$entityStorage protected property The content entity database storage used in this test.
EntityStorageDeprecationTest::$entityType protected property The mocked entity type used in this test.
EntityStorageDeprecationTest::$entityTypeBundleInfo protected property The mocked entity type bundle info used in this test.
EntityStorageDeprecationTest::$entityTypeId protected property The entity type ID.
EntityStorageDeprecationTest::$entityTypeManager protected property The mocked entity type manager used in this test.
EntityStorageDeprecationTest::$fieldDefinitions protected property An array of field definitions used for this test, keyed by field name.
EntityStorageDeprecationTest::$languageManager protected property The language manager.
EntityStorageDeprecationTest::$moduleHandler protected property The module handler.
EntityStorageDeprecationTest::setUp protected function Overrides UnitTestCase::setUp
EntityStorageDeprecationTest::setUpEntityStorage protected function Sets up the content entity storage.
EntityStorageDeprecationTest::testGetEntityClass public function Tests the deprecation when accessing entityClass directly.
EntityStorageDeprecationTest::testSetEntityClass public function Tests the deprecation when setting entityClass directly.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUpBeforeClass public static function

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