annotate core/modules/views/tests/src/Kernel/ViewsKernelTestBase.php @ 8:50b0d041100e

Further files for download
author Chris Cannam
date Mon, 05 Feb 2018 10:56:40 +0000
parents 4c8ae668cc8c
children 129ea1e6d783
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\views\Kernel;
Chris@0 4
Chris@0 5 use Drupal\Core\Database\Database;
Chris@0 6 use Drupal\Core\Database\Query\SelectInterface;
Chris@0 7 use Drupal\KernelTests\KernelTestBase;
Chris@0 8 use Drupal\views\Tests\ViewResultAssertionTrait;
Chris@0 9 use Drupal\views\Tests\ViewTestData;
Chris@0 10
Chris@0 11 /**
Chris@0 12 * Defines a base class for Views kernel testing.
Chris@0 13 */
Chris@0 14 abstract class ViewsKernelTestBase extends KernelTestBase {
Chris@0 15
Chris@0 16 use ViewResultAssertionTrait;
Chris@0 17
Chris@0 18 /**
Chris@0 19 * Views to be enabled.
Chris@0 20 *
Chris@0 21 * Test classes should override this property and provide the list of testing
Chris@0 22 * views.
Chris@0 23 *
Chris@0 24 * @var array
Chris@0 25 */
Chris@0 26 public static $testViews = [];
Chris@0 27
Chris@0 28 /**
Chris@0 29 * {@inheritdoc}
Chris@0 30 */
Chris@0 31 public static $modules = ['system', 'views', 'views_test_config', 'views_test_data', 'user'];
Chris@0 32
Chris@0 33 /**
Chris@0 34 * {@inheritdoc}
Chris@0 35 *
Chris@0 36 * @param bool $import_test_views
Chris@0 37 * Should the views specified on the test class be imported. If you need
Chris@0 38 * to setup some additional stuff, like fields, you need to call false and
Chris@0 39 * then call createTestViews for your own.
Chris@0 40 */
Chris@0 41 protected function setUp($import_test_views = TRUE) {
Chris@0 42 parent::setUp();
Chris@0 43
Chris@0 44 $this->installSchema('system', ['router', 'sequences', 'key_value_expire']);
Chris@0 45 $this->setUpFixtures();
Chris@0 46
Chris@0 47 if ($import_test_views) {
Chris@0 48 ViewTestData::createTestViews(get_class($this), ['views_test_config']);
Chris@0 49 }
Chris@0 50 }
Chris@0 51 /**
Chris@0 52 * Sets up the configuration and schema of views and views_test_data modules.
Chris@0 53 *
Chris@0 54 * Because the schema of views_test_data.module is dependent on the test
Chris@0 55 * using it, it cannot be enabled normally.
Chris@0 56 */
Chris@0 57 protected function setUpFixtures() {
Chris@0 58 // First install the system module. Many Views have Page displays have menu
Chris@0 59 // links, and for those to work, the system menus must already be present.
Chris@0 60 $this->installConfig(['system']);
Chris@0 61
Chris@0 62 /** @var \Drupal\Core\State\StateInterface $state */
Chris@0 63 $state = $this->container->get('state');
Chris@0 64 // Define the schema and views data variable before enabling the test module.
Chris@0 65 $state->set('views_test_data_schema', $this->schemaDefinition());
Chris@0 66 $state->set('views_test_data_views_data', $this->viewsData());
Chris@0 67
Chris@0 68 $this->installConfig(['views', 'views_test_config', 'views_test_data']);
Chris@0 69 foreach ($this->schemaDefinition() as $table => $schema) {
Chris@0 70 $this->installSchema('views_test_data', $table);
Chris@0 71 }
Chris@0 72
Chris@0 73 $this->container->get('router.builder')->rebuild();
Chris@0 74
Chris@0 75 // Load the test dataset.
Chris@0 76 $data_set = $this->dataSet();
Chris@0 77 $query = Database::getConnection()->insert('views_test_data')
Chris@0 78 ->fields(array_keys($data_set[0]));
Chris@0 79 foreach ($data_set as $record) {
Chris@0 80 $query->values($record);
Chris@0 81 }
Chris@0 82 $query->execute();
Chris@0 83 }
Chris@0 84
Chris@0 85 /**
Chris@0 86 * Orders a nested array containing a result set based on a given column.
Chris@0 87 *
Chris@0 88 * @param array $result_set
Chris@0 89 * An array of rows from a result set, with each row as an associative
Chris@0 90 * array keyed by column name.
Chris@0 91 * @param string $column
Chris@0 92 * The column name by which to sort the result set.
Chris@0 93 * @param bool $reverse
Chris@0 94 * (optional) Boolean indicating whether to sort the result set in reverse
Chris@0 95 * order. Defaults to FALSE.
Chris@0 96 *
Chris@0 97 * @return array
Chris@0 98 * The sorted result set.
Chris@0 99 */
Chris@0 100 protected function orderResultSet($result_set, $column, $reverse = FALSE) {
Chris@0 101 $order = $reverse ? -1 : 1;
Chris@0 102 usort($result_set, function ($a, $b) use ($column, $order) {
Chris@0 103 if ($a[$column] == $b[$column]) {
Chris@0 104 return 0;
Chris@0 105 }
Chris@0 106 return $order * (($a[$column] < $b[$column]) ? -1 : 1);
Chris@0 107 });
Chris@0 108 return $result_set;
Chris@0 109 }
Chris@0 110
Chris@0 111 /**
Chris@0 112 * Executes a view with debugging.
Chris@0 113 *
Chris@0 114 * @param \Drupal\views\ViewExecutable $view
Chris@0 115 * The view object.
Chris@0 116 * @param array $args
Chris@0 117 * (optional) An array of the view arguments to use for the view.
Chris@0 118 */
Chris@0 119 protected function executeView($view, array $args = []) {
Chris@0 120 $view->setDisplay();
Chris@0 121 $view->preExecute($args);
Chris@0 122 $view->execute();
Chris@0 123 $verbose_message = '<pre>Executed view: ' . ((string) $view->build_info['query']) . '</pre>';
Chris@0 124 if ($view->build_info['query'] instanceof SelectInterface) {
Chris@0 125 $verbose_message .= '<pre>Arguments: ' . print_r($view->build_info['query']->getArguments(), TRUE) . '</pre>';
Chris@0 126 }
Chris@0 127 $this->verbose($verbose_message);
Chris@0 128 }
Chris@0 129
Chris@0 130 /**
Chris@0 131 * Returns the schema definition.
Chris@0 132 *
Chris@0 133 * @internal
Chris@0 134 */
Chris@0 135 protected function schemaDefinition() {
Chris@0 136 return ViewTestData::schemaDefinition();
Chris@0 137 }
Chris@0 138
Chris@0 139 /**
Chris@0 140 * Returns the views data definition.
Chris@0 141 */
Chris@0 142 protected function viewsData() {
Chris@0 143 return ViewTestData::viewsData();
Chris@0 144 }
Chris@0 145
Chris@0 146 /**
Chris@0 147 * Returns a very simple test dataset.
Chris@0 148 */
Chris@0 149 protected function dataSet() {
Chris@0 150 return ViewTestData::dataSet();
Chris@0 151 }
Chris@0 152
Chris@0 153 }