View source
<?php
namespace Drupal\Tests\views\Kernel;
use Drupal\views\Exception\ViewRenderElementException;
use Drupal\views\Views;
class ViewElementTest extends ViewsKernelTestBase {
public static $testViews = [
'test_view_embed',
];
public function testViewElement() {
$renderer = $this->container
->get('renderer');
$view = Views::getView('test_view_embed');
$render = $view
->buildRenderable();
$this
->assertFalse($render['#embed']);
$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.');
$xpath = $this
->xpath('//div[@class="views-row"]');
$this
->assertCount(5, $xpath);
$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();
$view = Views::getView('test_view_embed');
$render = $view
->buildRenderable(NULL, [
25,
]);
$this
->setRawContent($renderer
->renderRoot($render));
$xpath = $this
->xpath('//div[@class="views-row"]');
$this
->assertCount(1, $xpath);
}
public function testViewElementEmbed() {
$renderer = $this->container
->get('renderer');
$view = Views::getView('test_view_embed');
$render = $view
->buildRenderable('embed_1');
$this
->assertTrue($render['#embed']);
$this
->setRawContent($renderer
->renderRoot($render));
serialize($render);
$xpath = $this
->xpath('//div[@class="views-element-container"]');
$this
->assertNotEmpty($xpath, 'The view container has been found in the rendered output.');
$xpath = $this
->xpath('//div[@class="views-row"]');
$this
->assertCount(5, $xpath);
$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();
$view = Views::getView('test_view_embed');
$render = $view
->buildRenderable('embed_1', [
25,
]);
$this
->setRawContent($renderer
->renderRoot($render));
$xpath = $this
->xpath('//div[@class="views-row"]');
$this
->assertCount(1, $xpath);
$view = Views::getView('test_view_embed');
$render = $view
->buildRenderable('embed_2');
$this
->setRawContent($renderer
->renderRoot($render));
$this
->assertCount(1, $this
->xpath('//form[@class="views-exposed-form"]'));
}
public function testInvalidView() {
$renderer = $this->container
->get('renderer');
$render_element = [
'#type' => 'view',
'#name' => 'invalid_view_name',
'#embed' => FALSE,
];
$this
->expectException(ViewRenderElementException::class);
$this
->expectExceptionMessage("Invalid View name ({$render_element['#name']}) given.");
$renderer
->renderRoot($render_element);
}
}