ViewTestBase.php in Drupal 8
File
core/modules/views/tests/src/Functional/ViewTestBase.php
View source
<?php
namespace Drupal\Tests\views\Functional;
use Behat\Mink\Exception\ElementNotFoundException;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Tests\BrowserTestBase;
use Drupal\views\Tests\ViewResultAssertionTrait;
use Drupal\views\Tests\ViewTestData;
use Drupal\views\ViewExecutable;
abstract class ViewTestBase extends BrowserTestBase {
use ViewResultAssertionTrait;
public static $modules = [
'views',
'views_test_config',
];
protected function setUp($import_test_views = TRUE) {
parent::setUp();
if ($import_test_views) {
ViewTestData::createTestViews(get_class($this), [
'views_test_config',
]);
}
}
protected function enableViewsTestModule() {
\Drupal::state()
->set('views_test_data_schema', $this
->schemaDefinition());
\Drupal::state()
->set('views_test_data_views_data', $this
->viewsData());
\Drupal::service('module_installer')
->install([
'views_test_data',
]);
$this
->resetAll();
$this
->rebuildContainer();
$this->container
->get('module_handler')
->reload();
$data_set = $this
->dataSet();
$query = Database::getConnection()
->insert('views_test_data')
->fields(array_keys($data_set[0]));
foreach ($data_set as $record) {
$query
->values($record);
}
$query
->execute();
}
protected function orderResultSet($result_set, $column, $reverse = FALSE) {
$order = $reverse ? -1 : 1;
usort($result_set, function ($a, $b) use ($column, $order) {
if ($a[$column] == $b[$column]) {
return 0;
}
return $order * ($a[$column] < $b[$column] ? -1 : 1);
});
return $result_set;
}
protected function helperButtonHasLabel($id, $expected_label, $message = 'Label has the expected value: %label.') {
$xpath = $this
->assertSession()
->buildXPathQuery('//button[@id=:value]|//input[@id=:value]', [
':value' => $id,
]);
$field = $this
->getSession()
->getPage()
->find('xpath', $xpath);
if (empty($field)) {
throw new ElementNotFoundException($this
->getSession()
->getDriver(), 'form field', 'id', $field);
}
$this
->assertEquals($expected_label, $field
->getValue());
}
protected function executeView(ViewExecutable $view, $args = []) {
$view
->setDisplay();
$view
->preExecute($args);
$view
->execute();
$verbose_message = '<pre>Executed view: ' . (string) $view->build_info['query'] . '</pre>';
if ($view->build_info['query'] instanceof SelectInterface) {
$verbose_message .= '<pre>Arguments: ' . print_r($view->build_info['query']
->getArguments(), TRUE) . '</pre>';
}
$this
->verbose($verbose_message);
}
protected function schemaDefinition() {
return ViewTestData::schemaDefinition();
}
protected function viewsData() {
return ViewTestData::viewsData();
}
protected function dataSet() {
return ViewTestData::dataSet();
}
}
Classes
Name |
Description |
ViewTestBase |
Defines a base class for Views testing in the full web test environment. |