abstract class ViewUnitTestBase

Defines a base class for Views unit testing.

Use this test class for unit tests of Views functionality. If a test requires the full web test environment provided by WebTestBase, extend ViewTestBase instead.

Hierarchy

Expanded class hierarchy of ViewUnitTestBase

See also

\Drupal\views\Tests\ViewTestBase

\Drupal\simpletest\DrupalUnitTestBase

25 files declare their use of ViewUnitTestBase
AreaTextTest.php in drupal/core/modules/views/lib/Drupal/views/Tests/Handler/AreaTextTest.php
Definition of Drupal\views\Tests\Handler\AreaTextTest.
ArgumentNullTest.php in drupal/core/modules/views/lib/Drupal/views/Tests/Handler/ArgumentNullTest.php
Definition of Drupal\views\Tests\Handler\ArgumentNullTest.
ArgumentValidatorTest.php in drupal/core/modules/views/lib/Drupal/views/Tests/Plugin/ArgumentValidatorTest.php
Definition of Drupal\views\Tests\Plugin\ArgumentValidatorTest.
FieldBooleanTest.php in drupal/core/modules/views/lib/Drupal/views/Tests/Handler/FieldBooleanTest.php
Definition of Drupal\views\Tests\Handler\FieldBooleanTest.
FieldCounterTest.php in drupal/core/modules/views/lib/Drupal/views/Tests/Handler/FieldCounterTest.php
Definition of Drupal\views\Tests\Handler\FieldCounterTest.

... See full list

File

drupal/core/modules/views/lib/Drupal/views/Tests/ViewUnitTestBase.php, line 25
Contains \Drupal\views\Tests\ViewUnitTestBase.

Namespace

Drupal\views\Tests
View source
abstract class ViewUnitTestBase extends DrupalUnitTestBase {
  protected function setUp() {
    parent::setUp();

    // Define the schema and views data variable before enabling the test module.
    $GLOBALS['views_test_data_schema'] = $this
      ->schemaDefinition();
    $GLOBALS['views_test_data_views_data'] = $this
      ->viewsData();
    $this
      ->enableModules(array(
      'views',
      'views_test_config',
      'views_test_data',
    ));

    // Load the test dataset.
    $data_set = $this
      ->dataSet();
    $query = db_insert('views_test_data')
      ->fields(array_keys($data_set[0]));
    foreach ($data_set as $record) {
      $query
        ->values($record);
    }
    $query
      ->execute();
    ViewTestData::importTestViews(get_class($this));
  }

  /**
   * Verifies that a result set returned by a View matches expected values.
   *
   * The comparison is done on the string representation of the columns of the
   * column map, taking the order of the rows into account, but not the order
   * of the columns.
   *
   * @param \Drupal\views\ViewExecutable $view
   *   An executed View.
   * @param array $expected_result
   *   An expected result set.
   * @param array $column_map
   *   (optional) An associative array mapping the columns of the result set
   *   from the view (as keys) and the expected result set (as values).
   * @param string $message
   *   (optional) A custom message to display with the assertion. Defaults to
   *   'Identical result set.'
   *
   * @return bool
   *   TRUE if the assertion succeeded, or FALSE otherwise.
   */
  protected function assertIdenticalResultset($view, $expected_result, $column_map = array(), $message = 'Identical result set') {
    return $this
      ->assertIdenticalResultsetHelper($view, $expected_result, $column_map, $message, 'assertIdentical');
  }

  /**
   * Verifies that a result set returned by a View differs from certain values.
   *
   * Inverse of ViewsTestCase::assertIdenticalResultset().
   *
   * @param \Drupal\views\ViewExecutable $view
   *   An executed View.
   * @param array $expected_result
   *   An expected result set.
   * @param array $column_map
   *   (optional) An associative array mapping the columns of the result set
   *  from the view (as keys) and the expected result set (as values).
   * @param string $message
   *   (optional) A custom message to display with the assertion. Defaults to
   *   'Non-identical result set.'
   *
   * @return bool
   *   TRUE if the assertion succeeded, or FALSE otherwise.
   */
  protected function assertNotIdenticalResultset($view, $expected_result, $column_map = array(), $message = 'Identical result set') {
    return $this
      ->assertIdenticalResultsetHelper($view, $expected_result, $column_map, $message, 'assertNotIdentical');
  }

  /**
   * Performs View result assertions.
   *
   * This is a helper method for ViewTestBase::assertIdenticalResultset() and
   * ViewTestBase::assertNotIdenticalResultset().
   *
   * @param \Drupal\views\ViewExecutable $view
   *   An executed View.
   * @param array $expected_result
   *   An expected result set.
   * @param array $column_map
   *   An associative array mapping the columns of the result set
   *   from the view (as keys) and the expected result set (as values).
   * @param string $message
   *   The message to display with the assertion.
   * @param string $assert_method
   *   The TestBase assertion method to use (either 'assertIdentical' or
   *   'assertNotIdentical').
   *
   * @return bool
   *   TRUE if the assertion succeeded, or FALSE otherwise.
   *
   * @see \Drupal\views\Tests\ViewTestBase::assertIdenticalResultset()
   * @see \Drupal\views\Tests\ViewTestBase::assertNotIdenticalResultset()
   */
  protected function assertIdenticalResultsetHelper($view, $expected_result, $column_map, $message, $assert_method) {

    // Convert $view->result to an array of arrays.
    $result = array();
    foreach ($view->result as $key => $value) {
      $row = array();
      foreach ($column_map as $view_column => $expected_column) {

        // The comparison will be done on the string representation of the value.
        $row[$expected_column] = (string) $value->{$view_column};
      }
      $result[$key] = $row;
    }

    // Remove the columns we don't need from the expected result.
    foreach ($expected_result as $key => $value) {
      $row = array();
      foreach ($column_map as $expected_column) {

        // The comparison will be done on the string representation of the value.
        $row[$expected_column] = (string) (is_object($value) ? $value->{$expected_column} : $value[$expected_column]);
      }
      $expected_result[$key] = $row;
    }

    // Reset the numbering of the arrays.
    $result = array_values($result);
    $expected_result = array_values($expected_result);
    $this
      ->verbose('<pre>Returned data set: ' . print_r($result, TRUE) . "\n\nExpected: " . print_r($expected_result, TRUE));

    // Do the actual comparison.
    return $this
      ->{$assert_method}($result, $expected_result, $message);
  }

  /**
   * Orders a nested array containing a result set based on a given column.
   *
   * @param array $result_set
   *   An array of rows from a result set, with each row as an associative
   *   array keyed by column name.
   * @param string $column
   *   The column name by which to sort the result set.
   * @param bool $reverse
   *   (optional) Boolean indicating whether to sort the result set in reverse
   *   order. Defaults to FALSE.
   *
   * @return array
   *   The sorted result set.
   */
  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;
  }

  /**
   * Executes a view with debugging.
   *
   * @param \Drupal\views\ViewExecutable $view
   *   The view object.
   * @param array $args
   *   (optional) An array of the view arguments to use for the view.
   */
  protected function executeView($view, $args = array()) {
    $view
      ->setDisplay();
    $view
      ->preExecute($args);
    $view
      ->execute();
    $this
      ->verbose('<pre>Executed view: ' . (string) $view->build_info['query'] . '</pre>');
  }

  /**
   * Returns the schema definition.
   */
  protected function schemaDefinition() {
    return ViewTestData::schemaDefinition();
  }

  /**
   * Returns the views data definition.
   */
  protected function viewsData() {
    return ViewTestData::viewsData();
  }

  /**
   * Returns a very simple test dataset.
   */
  protected function dataSet() {
    return ViewTestData::dataSet();
  }

}

Members

Namesort ascending Modifiers Type Description Overrides
ViewUnitTestBase::viewsData protected function Returns the views data definition. 17
ViewUnitTestBase::setUp protected function Sets up Drupal unit test environment. Overrides DrupalUnitTestBase::setUp 13
ViewUnitTestBase::schemaDefinition protected function Returns the schema definition. 4
ViewUnitTestBase::orderResultSet protected function Orders a nested array containing a result set based on a given column.
ViewUnitTestBase::executeView protected function Executes a view with debugging.
ViewUnitTestBase::dataSet protected function Returns a very simple test dataset. 6
ViewUnitTestBase::assertNotIdenticalResultset protected function Verifies that a result set returned by a View differs from certain values.
ViewUnitTestBase::assertIdenticalResultsetHelper protected function Performs View result assertions.
ViewUnitTestBase::assertIdenticalResultset protected function Verifies that a result set returned by a View matches expected values.
UnitTestBase::__construct function Constructor for UnitTestBase. Overrides TestBase::__construct 6
UnitTestBase::$configDirectories protected property
TestBase::verbose protected function Logs verbose message in a text file.
TestBase::tearDown protected function Deletes created files, database tables, and reverts all environment changes. 10
TestBase::run public function Run all tests in this class.
TestBase::rebuildContainer protected function Rebuild drupal_container().
TestBase::randomString public static function Generates a random string of ASCII characters of codes 32 to 126.
TestBase::randomObject public static function Generates a random PHP object.
TestBase::randomName public static function Generates a random string containing letters and numbers.
TestBase::prepareEnvironment protected function Prepares the current environment for running the test.
TestBase::prepareDatabasePrefix protected function Generates a database prefix for running tests.
TestBase::pass protected function Fire an assertion that is always positive.
TestBase::insertAssert public static function Store an assertion from outside the testing context.
TestBase::getDatabaseConnection public static function Returns the database connection to the site running Simpletest.
TestBase::getAssertionCall protected function Cycles through backtrace until the first non-assertion method is found.
TestBase::generatePermutations public static function Converts a list of possible parameters into a stack of permutations.
TestBase::filePreDeleteCallback public static function Ensures test files are deletable within file_unmanaged_delete_recursive().
TestBase::fail protected function Fire an assertion that is always negative.
TestBase::exceptionHandler protected function Handle exceptions.
TestBase::errorHandler public function Handle errors during test runs.
TestBase::error protected function Fire an error assertion. 1
TestBase::deleteAssert public static function Delete an assertion record by message ID.
TestBase::checkRequirements protected function Checks the matching requirements for Test. 3
TestBase::changeDatabasePrefix protected function Changes the database connection to the prefixed one.
TestBase::assertTrue protected function Check to see if a value is not false (not an empty string, 0, NULL, or FALSE).
TestBase::assertNull protected function Check to see if a value is NULL.
TestBase::assertNotNull protected function Check to see if a value is not NULL.
TestBase::assertNotIdentical protected function Check to see if two values are not identical.
TestBase::assertNotEqual protected function Check to see if two values are not equal.
TestBase::assertIdenticalObject protected function Checks to see if two objects are identical.
TestBase::assertIdentical protected function Check to see if two values are identical.
TestBase::assertFalse protected function Check to see if a value is false (an empty string, 0, NULL, or FALSE).
TestBase::assertEqual protected function Check to see if two values are equal.
TestBase::assert protected function Internal helper: stores the assert.
TestBase::$verboseId protected property Incrementing identifier for verbose output filenames.
TestBase::$verboseDirectoryUrl protected property URL to the verbose output file directory.
TestBase::$verboseDirectory protected property Directory where verbose output files are put.
TestBase::$verboseClassName protected property Safe class name for use in verbose output filenames.
TestBase::$verbose protected property TRUE if verbose debugging is enabled.
TestBase::$timeLimit protected property Time limit for the test.
TestBase::$testId protected property The test run ID.
TestBase::$skipClasses protected property This class is skipped when looking for the source of an assertion.
TestBase::$setupEnvironment protected property
TestBase::$setupDatabasePrefix protected property
TestBase::$setup protected property Flag to indicate whether the test has been set up.
TestBase::$results public property Current results of this test case.
TestBase::$originalPrefix protected property The original database prefix when running inside Simpletest.
TestBase::$originalFileDirectory protected property The original file directory, before it was changed for testing purposes.
TestBase::$databasePrefix protected property The database prefix of this test run.
TestBase::$assertions protected property Assertions thrown in that test case.
DrupalUnitTestBase::installSchema protected function Installs a specific table from a module schema definition.
DrupalUnitTestBase::enableModules protected function Enables modules for this test.
DrupalUnitTestBase::containerBuild public function Sets up the base service container for this test.
DrupalUnitTestBase::$themeFiles private property
DrupalUnitTestBase::$themeData private property
DrupalUnitTestBase::$modules public static property Modules to enable. 6
DrupalUnitTestBase::$moduleList private property Fixed module list being used by this test.
DrupalUnitTestBase::$moduleFiles private property