function ViewElementTest::testViewElement

Same name in this branch
  1. 8.9.x core/modules/views/tests/src/Functional/ViewElementTest.php \Drupal\Tests\views\Functional\ViewElementTest::testViewElement()
Same name in other branches
  1. 9 core/modules/views/tests/src/Kernel/ViewElementTest.php \Drupal\Tests\views\Kernel\ViewElementTest::testViewElement()
  2. 9 core/modules/views/tests/src/Functional/ViewElementTest.php \Drupal\Tests\views\Functional\ViewElementTest::testViewElement()
  3. 10 core/modules/views/tests/src/Kernel/ViewElementTest.php \Drupal\Tests\views\Kernel\ViewElementTest::testViewElement()
  4. 10 core/modules/views/tests/src/Functional/ViewElementTest.php \Drupal\Tests\views\Functional\ViewElementTest::testViewElement()
  5. 11.x core/modules/views/tests/src/Kernel/ViewElementTest.php \Drupal\Tests\views\Kernel\ViewElementTest::testViewElement()
  6. 11.x core/modules/views/tests/src/Functional/ViewElementTest.php \Drupal\Tests\views\Functional\ViewElementTest::testViewElement()

Tests the rendered output and form output of a view element.

File

core/modules/views/tests/src/Kernel/ViewElementTest.php, line 24

Class

ViewElementTest
Tests the view render element.

Namespace

Drupal\Tests\views\Kernel

Code

public function testViewElement() {
    
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = $this->container
        ->get('renderer');
    $view = Views::getView('test_view_embed');
    // Get the render array, #embed must be FALSE since this is the default
    // display.
    $render = $view->buildRenderable();
    $this->assertEqual($render['#embed'], FALSE);
    $this->setRawContent($renderer->renderRoot($render));
    $xpath = $this->xpath('//div[@class="views-element-container"]');
    $this->assertNotEmpty($xpath, 'The view container has been found in the rendered output.');
    // There should be 5 rows in the results.
    $xpath = $this->xpath('//div[@class="views-row"]');
    $this->assertCount(5, $xpath);
    // Add an argument and save the view.
    $view->displayHandlers
        ->get('default')
        ->overrideOption('arguments', [
        'age' => [
            'default_action' => 'ignore',
            'title' => '',
            'default_argument_type' => 'fixed',
            'validate' => [
                'type' => 'none',
                'fail' => 'not found',
            ],
            'break_phrase' => FALSE,
            'not' => FALSE,
            'id' => 'age',
            'table' => 'views_test_data',
            'field' => 'age',
            'plugin_id' => 'numeric',
        ],
    ]);
    $view->save();
    // Test the render array again.
    $view = Views::getView('test_view_embed');
    $render = $view->buildRenderable(NULL, [
        25,
    ]);
    $this->setRawContent($renderer->renderRoot($render));
    // There should be 1 row in the results, 'John' arg 25.
    $xpath = $this->xpath('//div[@class="views-row"]');
    $this->assertCount(1, $xpath);
}

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