class EntityRepositoryTest

Same name in this branch
  1. 11.x core/tests/Drupal/KernelTests/Core/Entity/EntityRepositoryTest.php \Drupal\KernelTests\Core\Entity\EntityRepositoryTest
Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityRepositoryTest.php \Drupal\KernelTests\Core\Entity\EntityRepositoryTest
  2. 9 core/tests/Drupal/Tests/Core/Entity/EntityRepositoryTest.php \Drupal\Tests\Core\Entity\EntityRepositoryTest
  3. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityRepositoryTest.php \Drupal\KernelTests\Core\Entity\EntityRepositoryTest
  4. 8.9.x core/tests/Drupal/Tests/Core/Entity/EntityRepositoryTest.php \Drupal\Tests\Core\Entity\EntityRepositoryTest
  5. 10 core/tests/Drupal/KernelTests/Core/Entity/EntityRepositoryTest.php \Drupal\KernelTests\Core\Entity\EntityRepositoryTest
  6. 10 core/tests/Drupal/Tests/Core/Entity/EntityRepositoryTest.php \Drupal\Tests\Core\Entity\EntityRepositoryTest

@coversDefaultClass \Drupal\Core\Entity\EntityRepository @group Entity

Hierarchy

Expanded class hierarchy of EntityRepositoryTest

File

core/tests/Drupal/Tests/Core/Entity/EntityRepositoryTest.php, line 21

Namespace

Drupal\Tests\Core\Entity
View source
class EntityRepositoryTest extends UnitTestCase {
    
    /**
     * The entity type manager.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\Prophecy\Prophecy\ProphecyInterface
     */
    protected $entityTypeManager;
    
    /**
     * The language manager.
     *
     * @var \Drupal\Core\Language\LanguageManagerInterface|\Prophecy\Prophecy\ProphecyInterface
     */
    protected $languageManager;
    
    /**
     * The context repository.
     *
     * @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface|\Prophecy\Prophecy\ProphecyInterface
     */
    protected $contextRepository;
    
    /**
     * The entity repository under test.
     *
     * @var \Drupal\Core\Entity\EntityRepository
     */
    protected $entityRepository;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->entityTypeManager = $this->prophesize(EntityTypeManagerInterface::class);
        $this->languageManager = $this->prophesize(LanguageManagerInterface::class);
        $this->contextRepository = $this->prophesize(ContextRepositoryInterface::class);
        $this->entityRepository = new EntityRepository($this->entityTypeManager
            ->reveal(), $this->languageManager
            ->reveal(), $this->contextRepository
            ->reveal());
    }
    
    /**
     * Tests the getTranslationFromContext() method.
     *
     * @covers ::getTranslationFromContext
     */
    public function testGetTranslationFromContext() : void {
        $language = new Language([
            'id' => 'en',
        ]);
        $this->languageManager
            ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
            ->willReturn($language)
            ->shouldBeCalledTimes(1);
        $this->languageManager
            ->getFallbackCandidates(Argument::type('array'))
            ->will(function ($args) {
            $context = $args[0];
            $candidates = [];
            if (!empty($context['langcode'])) {
                $candidates[$context['langcode']] = $context['langcode'];
            }
            return $candidates;
        })
            ->shouldBeCalledTimes(1);
        $translated_entity = $this->prophesize(ContentEntityInterface::class);
        $entity = $this->prophesize(ContentEntityInterface::class);
        $entity->getUntranslated()
            ->willReturn($entity);
        $entity->language()
            ->willReturn($language);
        $entity->hasTranslation(LanguageInterface::LANGCODE_DEFAULT)
            ->willReturn(FALSE);
        $entity->hasTranslation('custom_langcode')
            ->willReturn(TRUE);
        $entity->getTranslation('custom_langcode')
            ->willReturn($translated_entity->reveal());
        $entity->getTranslationLanguages()
            ->willReturn([
            new Language([
                'id' => 'en',
            ]),
            new Language([
                'id' => 'custom_langcode',
            ]),
        ]);
        $entity->addCacheContexts([
            'languages:language_content',
        ])
            ->shouldBeCalled();
        $this->assertSame($entity->reveal(), $this->entityRepository
            ->getTranslationFromContext($entity->reveal()));
        $this->assertSame($translated_entity->reveal(), $this->entityRepository
            ->getTranslationFromContext($entity->reveal(), 'custom_langcode'));
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
EntityRepositoryTest::$contextRepository protected property The context repository.
EntityRepositoryTest::$entityRepository protected property The entity repository under test.
EntityRepositoryTest::$entityTypeManager protected property The entity type manager.
EntityRepositoryTest::$languageManager protected property The language manager.
EntityRepositoryTest::setUp protected function Overrides UnitTestCase::setUp
EntityRepositoryTest::testGetTranslationFromContext public function Tests the getTranslationFromContext() method.
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
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::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function

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