You are here

class EntityListBuilderTest in Drupal 9

Same name in this branch
  1. 9 core/tests/Drupal/Tests/Core/Entity/EntityListBuilderTest.php \Drupal\Tests\Core\Entity\EntityListBuilderTest
  2. 9 core/modules/system/tests/src/Functional/Entity/EntityListBuilderTest.php \Drupal\Tests\system\Functional\Entity\EntityListBuilderTest
Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Entity/EntityListBuilderTest.php \Drupal\Tests\Core\Entity\EntityListBuilderTest
  2. 10 core/tests/Drupal/Tests/Core/Entity/EntityListBuilderTest.php \Drupal\Tests\Core\Entity\EntityListBuilderTest

@coversDefaultClass \Drupal\entity_test\EntityTestListBuilder @group Entity

Hierarchy

Expanded class hierarchy of EntityListBuilderTest

File

core/tests/Drupal/Tests/Core/Entity/EntityListBuilderTest.php, line 22
Contains \Drupal\Tests\Core\Entity\EntityListBuilderTest.

Namespace

Drupal\Tests\Core\Entity
View source
class EntityListBuilderTest extends UnitTestCase {

  /**
   * The entity type used for testing.
   *
   * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $entityType;

  /**
   * The module handler used for testing.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $moduleHandler;

  /**
   * The translation manager used for testing.
   *
   * @var \Drupal\Core\StringTranslation\TranslationInterface
   */
  protected $translationManager;

  /**
   * The role storage used for testing.
   *
   * @var \Drupal\user\RoleStorageInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $roleStorage;

  /**
   * The service container used for testing.
   *
   * @var \Drupal\Core\DependencyInjection\ContainerBuilder
   */
  protected $container;

  /**
   * The entity used to construct the EntityListBuilder.
   *
   * @var \Drupal\user\RoleInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $role;

  /**
   * The redirect destination service.
   *
   * @var \Drupal\Core\Routing\RedirectDestinationInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $redirectDestination;

  /**
   * The EntityListBuilder object to test.
   *
   * @var \Drupal\Core\Entity\EntityListBuilder
   */
  protected $entityListBuilder;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->role = $this
      ->createMock('Drupal\\user\\RoleInterface');
    $this->roleStorage = $this
      ->createMock('\\Drupal\\user\\RoleStorageInterface');
    $this->moduleHandler = $this
      ->createMock('\\Drupal\\Core\\Extension\\ModuleHandlerInterface');
    $this->entityType = $this
      ->createMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
    $this->translationManager = $this
      ->createMock('\\Drupal\\Core\\StringTranslation\\TranslationInterface');
    $this->entityListBuilder = new TestEntityListBuilder($this->entityType, $this->roleStorage);
    $this->redirectDestination = $this
      ->createMock(RedirectDestinationInterface::class);
    $this->container = new ContainerBuilder();
    \Drupal::setContainer($this->container);
  }

  /**
   * @covers ::getOperations
   */
  public function testGetOperations() {
    $operation_name = $this
      ->randomMachineName();
    $operations = [
      $operation_name => [
        'title' => $this
          ->randomMachineName(),
      ],
    ];
    $this->moduleHandler
      ->expects($this
      ->once())
      ->method('invokeAll')
      ->with('entity_operation', [
      $this->role,
    ])
      ->will($this
      ->returnValue($operations));
    $this->moduleHandler
      ->expects($this
      ->once())
      ->method('alter')
      ->with('entity_operation');
    $this->container
      ->set('module_handler', $this->moduleHandler);
    $this->role
      ->expects($this
      ->any())
      ->method('access')
      ->will($this
      ->returnValue(AccessResult::allowed()));
    $this->role
      ->expects($this
      ->any())
      ->method('hasLinkTemplate')
      ->will($this
      ->returnValue(TRUE));
    $url = $this
      ->getMockBuilder('\\Drupal\\Core\\Url')
      ->disableOriginalConstructor()
      ->getMock();
    $url
      ->expects($this
      ->atLeastOnce())
      ->method('mergeOptions')
      ->with([
      'query' => [
        'destination' => '/foo/bar',
      ],
    ]);
    $this->role
      ->expects($this
      ->any())
      ->method('toUrl')
      ->will($this
      ->returnValue($url));
    $this->redirectDestination
      ->expects($this
      ->atLeastOnce())
      ->method('getAsArray')
      ->willReturn([
      'destination' => '/foo/bar',
    ]);
    $list = new EntityListBuilder($this->entityType, $this->roleStorage);
    $list
      ->setStringTranslation($this->translationManager);
    $list
      ->setRedirectDestination($this->redirectDestination);
    $operations = $list
      ->getOperations($this->role);
    $this
      ->assertIsArray($operations);
    $this
      ->assertArrayHasKey('edit', $operations);
    $this
      ->assertIsArray($operations['edit']);
    $this
      ->assertArrayHasKey('title', $operations['edit']);
    $this
      ->assertArrayHasKey('delete', $operations);
    $this
      ->assertIsArray($operations['delete']);
    $this
      ->assertArrayHasKey('title', $operations['delete']);
    $this
      ->assertArrayHasKey($operation_name, $operations);
    $this
      ->assertIsArray($operations[$operation_name]);
    $this
      ->assertArrayHasKey('title', $operations[$operation_name]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityListBuilderTest::$container protected property The service container used for testing.
EntityListBuilderTest::$entityListBuilder protected property The EntityListBuilder object to test.
EntityListBuilderTest::$entityType protected property The entity type used for testing.
EntityListBuilderTest::$moduleHandler protected property The module handler used for testing.
EntityListBuilderTest::$redirectDestination protected property The redirect destination service.
EntityListBuilderTest::$role protected property The entity used to construct the EntityListBuilder.
EntityListBuilderTest::$roleStorage protected property The role storage used for testing.
EntityListBuilderTest::$translationManager protected property The translation manager used for testing.
EntityListBuilderTest::setUp protected function Overrides UnitTestCase::setUp
EntityListBuilderTest::testGetOperations public function @covers ::getOperations
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