class ViewsTest

Same name in other branches
  1. 8.9.x core/modules/views/tests/src/Unit/ViewsTest.php \Drupal\Tests\views\Unit\ViewsTest
  2. 10 core/modules/views/tests/src/Unit/ViewsTest.php \Drupal\Tests\views\Unit\ViewsTest
  3. 11.x core/modules/views/tests/src/Unit/ViewsTest.php \Drupal\Tests\views\Unit\ViewsTest

@coversDefaultClass \Drupal\views\Views @group views

Hierarchy

Expanded class hierarchy of ViewsTest

File

core/modules/views/tests/src/Unit/ViewsTest.php, line 19

Namespace

Drupal\Tests\views\Unit
View source
class ViewsTest extends UnitTestCase {
    
    /**
     * The test container.
     *
     * @var \Drupal\Core\DependencyInjection\ContainerBuilder
     */
    protected $container;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->container = new ContainerBuilder();
        $user = $this->createMock('Drupal\\Core\\Session\\AccountInterface');
        $request_stack = new RequestStack();
        $request_stack->push(new Request());
        $views_data = $this->getMockBuilder('Drupal\\views\\ViewsData')
            ->disableOriginalConstructor()
            ->getMock();
        $route_provider = $this->createMock('Drupal\\Core\\Routing\\RouteProviderInterface');
        $this->container
            ->set('views.executable', new ViewExecutableFactory($user, $request_stack, $views_data, $route_provider));
        \Drupal::setContainer($this->container);
    }
    
    /**
     * Tests the getView() method.
     *
     * @covers ::getView
     */
    public function testGetView() {
        $view = new View([
            'id' => 'test_view',
        ], 'view');
        $view_storage = $this->getMockBuilder('Drupal\\Core\\Config\\Entity\\ConfigEntityStorage')
            ->disableOriginalConstructor()
            ->getMock();
        $view_storage->expects($this->once())
            ->method('load')
            ->with('test_view')
            ->willReturn($view);
        $entity_type_manager = $this->createMock('Drupal\\Core\\Entity\\EntityTypeManagerInterface');
        $entity_type_manager->expects($this->once())
            ->method('getStorage')
            ->with('view')
            ->willReturn($view_storage);
        $this->container
            ->set('entity_type.manager', $entity_type_manager);
        $executable = Views::getView('test_view');
        $this->assertInstanceOf('Drupal\\views\\ViewExecutable', $executable);
        $this->assertEquals($view->id(), $executable->storage
            ->id());
        $this->assertEquals(spl_object_hash($view), spl_object_hash($executable->storage));
    }
    
    /**
     * Tests the getView() method against a non-existent view.
     *
     * @covers ::getView
     */
    public function testGetNonExistentView() {
        $entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class);
        $storage = $this->prophesize(EntityStorageInterface::class);
        $storage->load('test_view_non_existent')
            ->willReturn(NULL);
        $entity_type_manager->getStorage('view')
            ->willReturn($storage->reveal());
        $this->container
            ->set('entity_type.manager', $entity_type_manager->reveal());
        $executable_does_not_exist = Views::getView('test_view_non_existent');
        $this->assertNull($executable_does_not_exist);
    }
    
    /**
     * @covers ::getApplicableViews
     *
     * @dataProvider providerTestGetApplicableViews
     */
    public function testGetApplicableViews($applicable_type, $expected) {
        $view_1 = new View([
            'id' => 'test_view_1',
            'display' => [
                'default' => [
                    'display_plugin' => 'default',
                    'display_options' => [],
                ],
                'type_a' => [
                    'display_plugin' => 'type_a',
                    'display_options' => [],
                ],
            ],
        ], 'view');
        $view_2 = new View([
            'id' => 'test_view_2',
            'display' => [
                'default' => [
                    'display_plugin' => 'default',
                    'display_options' => [],
                ],
                'type_b' => [
                    'display_plugin' => 'type_b',
                    'display_options' => [
                        'enabled' => TRUE,
                    ],
                ],
                'type_b_2' => [
                    'display_plugin' => 'type_b',
                    'display_options' => [
                        'enabled' => FALSE,
                    ],
                ],
            ],
        ], 'view');
        $view_3 = new View([
            'id' => 'test_view_3',
            'display' => [
                'default' => [
                    'display_plugin' => 'default',
                    'display_options' => [],
                ],
                // Test with Type A but a disabled display.
'type_a' => [
                    'display_plugin' => 'type_a',
                    'display_options' => [
                        'enabled' => FALSE,
                    ],
                ],
                // Type D intentionally doesn't exist.
'type_d' => [
                    'display_plugin' => 'type_d',
                    'display_options' => [],
                ],
            ],
        ], 'view');
        $query = $this->createMock('Drupal\\Core\\Entity\\Query\\QueryInterface');
        $query->expects($this->exactly(2))
            ->method('condition')
            ->willReturnSelf();
        $query->expects($this->once())
            ->method('execute')
            ->willReturn([
            'test_view_1',
            'test_view_2',
            'test_view_3',
        ]);
        $view_storage = $this->getMockBuilder('Drupal\\Core\\Config\\Entity\\ConfigEntityStorage')
            ->disableOriginalConstructor()
            ->getMock();
        $view_storage->expects($this->once())
            ->method('getQuery')
            ->willReturn($query);
        $view_storage->expects($this->once())
            ->method('loadMultiple')
            ->with([
            'test_view_1',
            'test_view_2',
            'test_view_3',
        ])
            ->willReturn([
            'test_view_1' => $view_1,
            'test_view_2' => $view_2,
            'test_view_3' => $view_3,
        ]);
        $entity_type_manager = $this->createMock(EntityTypeManagerInterface::class);
        $entity_type_manager->expects($this->exactly(2))
            ->method('getStorage')
            ->with('view')
            ->willReturn($view_storage);
        $this->container
            ->set('entity_type.manager', $entity_type_manager);
        $definitions = [
            'type_a' => [
                'type_a' => TRUE,
                'type_b' => FALSE,
            ],
            'type_b' => [
                'type_a' => FALSE,
                'type_b' => TRUE,
            ],
        ];
        $display_manager = $this->createMock('Drupal\\Component\\Plugin\\PluginManagerInterface');
        $display_manager->expects($this->once())
            ->method('getDefinitions')
            ->willReturn($definitions);
        $this->container
            ->set('plugin.manager.views.display', $display_manager);
        $result = Views::getApplicableViews($applicable_type);
        $this->assertEquals($expected, $result);
    }
    
    /**
     * Data provider for testGetApplicableViews.
     *
     * @return array
     */
    public function providerTestGetApplicableViews() {
        return [
            [
                'type_a',
                [
                    [
                        'test_view_1',
                        'type_a',
                    ],
                ],
            ],
            [
                'type_b',
                [
                    [
                        'test_view_2',
                        'type_b',
                    ],
                ],
            ],
            [
                'type_c',
                [],
            ],
        ];
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
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
ViewsTest::$container protected property The test container.
ViewsTest::providerTestGetApplicableViews public function Data provider for testGetApplicableViews.
ViewsTest::setUp protected function Overrides UnitTestCase::setUp
ViewsTest::testGetApplicableViews public function @covers ::getApplicableViews
ViewsTest::testGetNonExistentView public function Tests the getView() method against a non-existent view.
ViewsTest::testGetView public function Tests the getView() method.

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