class EntityOperationsUnitTest

Same name in other branches
  1. 9 core/modules/views/tests/src/Unit/Plugin/views/field/EntityOperationsUnitTest.php \Drupal\Tests\views\Unit\Plugin\views\field\EntityOperationsUnitTest
  2. 8.9.x core/modules/views/tests/src/Unit/Plugin/views/field/EntityOperationsUnitTest.php \Drupal\Tests\views\Unit\Plugin\views\field\EntityOperationsUnitTest
  3. 11.x core/modules/views/tests/src/Unit/Plugin/views/field/EntityOperationsUnitTest.php \Drupal\Tests\views\Unit\Plugin\views\field\EntityOperationsUnitTest

@coversDefaultClass \Drupal\views\Plugin\views\field\EntityOperations @group Views

Hierarchy

Expanded class hierarchy of EntityOperationsUnitTest

File

core/modules/views/tests/src/Unit/Plugin/views/field/EntityOperationsUnitTest.php, line 18

Namespace

Drupal\Tests\views\Unit\Plugin\views\field
View source
class EntityOperationsUnitTest extends UnitTestCase {
    use ViewsLoggerTestTrait;
    
    /**
     * The entity type manager.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $entityTypeManager;
    
    /**
     * The entity repository.
     *
     * @var \Drupal\Core\Entity\EntityRepositoryInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $entityRepository;
    
    /**
     * The language manager.
     *
     * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $languageManager;
    
    /**
     * The plugin under test.
     *
     * @var \Drupal\views\Plugin\views\field\EntityOperations
     */
    protected $plugin;
    
    /**
     * {@inheritdoc}
     *
     * @covers ::__construct
     */
    protected function setUp() : void {
        parent::setUp();
        $this->entityTypeManager = $this->createMock(EntityTypeManagerInterface::class);
        $this->entityRepository = $this->createMock(EntityRepositoryInterface::class);
        $this->languageManager = $this->createMock('\\Drupal\\Core\\Language\\LanguageManagerInterface');
        $configuration = [
            'entity_type' => 'foo',
            'entity field' => 'bar',
        ];
        $plugin_id = $this->randomMachineName();
        $plugin_definition = [
            'title' => $this->randomMachineName(),
        ];
        $this->plugin = new EntityOperations($configuration, $plugin_id, $plugin_definition, $this->entityTypeManager, $this->languageManager, $this->entityRepository);
        $redirect_service = $this->createMock('Drupal\\Core\\Routing\\RedirectDestinationInterface');
        $redirect_service->expects($this->any())
            ->method('getAsArray')
            ->willReturn([
            'destination' => 'foobar',
        ]);
        $this->plugin
            ->setRedirectDestination($redirect_service);
        $view = $this->getMockBuilder('\\Drupal\\views\\ViewExecutable')
            ->disableOriginalConstructor()
            ->getMock();
        $display = $this->getMockBuilder('\\Drupal\\views\\Plugin\\views\\display\\DisplayPluginBase')
            ->disableOriginalConstructor()
            ->getMockForAbstractClass();
        $view->display_handler = $display;
        $this->plugin
            ->init($view, $display);
    }
    
    /**
     * @covers ::usesGroupBy
     */
    public function testUsesGroupBy() : void {
        $this->assertFalse($this->plugin
            ->usesGroupBy());
    }
    
    /**
     * @covers ::defineOptions
     */
    public function testDefineOptions() : void {
        $options = $this->plugin
            ->defineOptions();
        $this->assertIsArray($options);
        $this->assertArrayHasKey('destination', $options);
    }
    
    /**
     * @covers ::render
     */
    public function testRenderWithDestination() : void {
        $entity_type_id = $this->randomMachineName();
        $entity = $this->getMockBuilder('\\Drupal\\user\\Entity\\Role')
            ->disableOriginalConstructor()
            ->getMock();
        $entity->expects($this->any())
            ->method('getEntityTypeId')
            ->willReturn($entity_type_id);
        $operations = [
            'foo' => [
                'title' => $this->randomMachineName(),
            ],
        ];
        $list_builder = $this->createMock('\\Drupal\\Core\\Entity\\EntityListBuilderInterface');
        $list_builder->expects($this->once())
            ->method('getOperations')
            ->with($entity)
            ->willReturn($operations);
        $this->entityTypeManager
            ->expects($this->once())
            ->method('getListBuilder')
            ->with($entity_type_id)
            ->willReturn($list_builder);
        $this->plugin->options['destination'] = TRUE;
        $result = new ResultRow();
        $result->_entity = $entity;
        $expected_build = [
            '#type' => 'operations',
            '#links' => $operations,
        ];
        $expected_build['#links']['foo']['query'] = [
            'destination' => 'foobar',
        ];
        $build = $this->plugin
            ->render($result);
        $this->assertSame($expected_build, $build);
    }
    
    /**
     * @covers ::render
     */
    public function testRenderWithoutDestination() : void {
        $entity_type_id = $this->randomMachineName();
        $entity = $this->getMockBuilder('\\Drupal\\user\\Entity\\Role')
            ->disableOriginalConstructor()
            ->getMock();
        $entity->expects($this->any())
            ->method('getEntityTypeId')
            ->willReturn($entity_type_id);
        $operations = [
            'foo' => [
                'title' => $this->randomMachineName(),
            ],
        ];
        $list_builder = $this->createMock('\\Drupal\\Core\\Entity\\EntityListBuilderInterface');
        $list_builder->expects($this->once())
            ->method('getOperations')
            ->with($entity)
            ->willReturn($operations);
        $this->entityTypeManager
            ->expects($this->once())
            ->method('getListBuilder')
            ->with($entity_type_id)
            ->willReturn($list_builder);
        $this->plugin->options['destination'] = FALSE;
        $result = new ResultRow();
        $result->_entity = $entity;
        $expected_build = [
            '#type' => 'operations',
            '#links' => $operations,
        ];
        $build = $this->plugin
            ->render($result);
        $this->assertSame($expected_build, $build);
    }
    
    /**
     * @covers ::render
     */
    public function testRenderWithoutEntity() : void {
        $this->setUpMockLoggerWithMissingEntity();
        $entity = NULL;
        $result = new ResultRow();
        $result->_entity = $entity;
        $expected_build = '';
        $build = $this->plugin
            ->render($result);
        $this->assertSame($expected_build, $build);
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
EntityOperationsUnitTest::$entityRepository protected property The entity repository.
EntityOperationsUnitTest::$entityTypeManager protected property The entity type manager.
EntityOperationsUnitTest::$languageManager protected property The language manager.
EntityOperationsUnitTest::$plugin protected property The plugin under test.
EntityOperationsUnitTest::setUp protected function @covers ::__construct Overrides UnitTestCase::setUp
EntityOperationsUnitTest::testDefineOptions public function @covers ::defineOptions
EntityOperationsUnitTest::testRenderWithDestination public function @covers ::render
EntityOperationsUnitTest::testRenderWithoutDestination public function @covers ::render
EntityOperationsUnitTest::testRenderWithoutEntity public function @covers ::render
EntityOperationsUnitTest::testUsesGroupBy public function @covers ::usesGroupBy
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.
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.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
UnitTestCase::$root protected property The app root. 1
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
UnitTestCase::__get public function
ViewsLoggerTestTrait::setUpMockLoggerWithMissingEntity public function Sets up a mock logger for when views can't load an entity.

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