PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/core/modules/views/src/Tests/ViewTestBase.php

https://gitlab.com/geeta7/drupal
PHP | 154 lines | 62 code | 17 blank | 75 comment | 4 complexity | 4005d9b8a233d8e754f70aea57278e5b MD5 | raw file
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\views\Tests\ViewTestBase.
  5. */
  6. namespace Drupal\views\Tests;
  7. use Drupal\Core\Database\Query\SelectInterface;
  8. use Drupal\simpletest\WebTestBase;
  9. use Drupal\views\ViewExecutable;
  10. /**
  11. * Defines a base class for Views testing in the full web test environment.
  12. *
  13. * Use this base test class if you need to emulate a full Drupal installation.
  14. * When possible, ViewKernelTestBase should be used instead. Both base classes
  15. * include the same methods.
  16. *
  17. * @see \Drupal\views\Tests\ViewKernelTestBase
  18. * @see \Drupal\simpletest\WebTestBase
  19. */
  20. abstract class ViewTestBase extends WebTestBase {
  21. use ViewResultAssertionTrait;
  22. /**
  23. * Modules to enable.
  24. *
  25. * @var array
  26. */
  27. public static $modules = array('views', 'views_test_config');
  28. protected function setUp($import_test_views = TRUE) {
  29. parent::setUp();
  30. if ($import_test_views) {
  31. ViewTestData::createTestViews(get_class($this), array('views_test_config'));
  32. }
  33. }
  34. /**
  35. * Sets up the views_test_data.module.
  36. *
  37. * Because the schema of views_test_data.module is dependent on the test
  38. * using it, it cannot be enabled normally.
  39. */
  40. protected function enableViewsTestModule() {
  41. // Define the schema and views data variable before enabling the test module.
  42. \Drupal::state()->set('views_test_data_schema', $this->schemaDefinition());
  43. \Drupal::state()->set('views_test_data_views_data', $this->viewsData());
  44. \Drupal::service('module_installer')->install(array('views_test_data'));
  45. $this->resetAll();
  46. $this->rebuildContainer();
  47. $this->container->get('module_handler')->reload();
  48. // Load the test dataset.
  49. $data_set = $this->dataSet();
  50. $query = db_insert('views_test_data')
  51. ->fields(array_keys($data_set[0]));
  52. foreach ($data_set as $record) {
  53. $query->values($record);
  54. }
  55. $query->execute();
  56. }
  57. /**
  58. * Orders a nested array containing a result set based on a given column.
  59. *
  60. * @param array $result_set
  61. * An array of rows from a result set, with each row as an associative
  62. * array keyed by column name.
  63. * @param string $column
  64. * The column name by which to sort the result set.
  65. * @param bool $reverse
  66. * (optional) Boolean indicating whether to sort the result set in reverse
  67. * order. Defaults to FALSE.
  68. *
  69. * @return array
  70. * The sorted result set.
  71. */
  72. protected function orderResultSet($result_set, $column, $reverse = FALSE) {
  73. $order = $reverse ? -1 : 1;
  74. usort($result_set, function ($a, $b) use ($column, $order) {
  75. if ($a[$column] == $b[$column]) {
  76. return 0;
  77. }
  78. return $order * (($a[$column] < $b[$column]) ? -1 : 1);
  79. });
  80. return $result_set;
  81. }
  82. /**
  83. * Asserts the existence of a button with a certain ID and label.
  84. *
  85. * @param string $id
  86. * The HTML ID of the button
  87. * @param string $label.
  88. * The expected label for the button.
  89. * @param string $message
  90. * (optional) A custom message to display with the assertion. If no custom
  91. * message is provided, the message will indicate the button label.
  92. *
  93. * @return bool
  94. * TRUE if the assertion was successful, or FALSE on failure.
  95. */
  96. protected function helperButtonHasLabel($id, $expected_label, $message = 'Label has the expected value: %label.') {
  97. return $this->assertFieldById($id, $expected_label, t($message, array('%label' => $expected_label)));
  98. }
  99. /**
  100. * Executes a view with debugging.
  101. *
  102. * @param \Drupal\views\ViewExecutable $view
  103. * The view object.
  104. * @param array $args
  105. * (optional) An array of the view arguments to use for the view.
  106. */
  107. protected function executeView(ViewExecutable $view, $args = array()) {
  108. // A view does not really work outside of a request scope, due to many
  109. // dependencies like the current user.
  110. $view->setDisplay();
  111. $view->preExecute($args);
  112. $view->execute();
  113. $verbose_message = '<pre>Executed view: ' . ((string) $view->build_info['query']). '</pre>';
  114. if ($view->build_info['query'] instanceof SelectInterface) {
  115. $verbose_message .= '<pre>Arguments: ' . print_r($view->build_info['query']->getArguments(), TRUE) . '</pre>';
  116. }
  117. $this->verbose($verbose_message);
  118. }
  119. /**
  120. * Returns the schema definition.
  121. */
  122. protected function schemaDefinition() {
  123. return ViewTestData::schemaDefinition();
  124. }
  125. /**
  126. * Returns the views data definition.
  127. */
  128. protected function viewsData() {
  129. return ViewTestData::viewsData();
  130. }
  131. /**
  132. * Returns a very simple test dataset.
  133. */
  134. protected function dataSet() {
  135. return ViewTestData::dataSet();
  136. }
  137. }