diff --git a/core/modules/views/lib/Drupal/views/Tests/Handler/FieldUnitTest.php b/core/modules/views/lib/Drupal/views/Tests/Handler/FieldUnitTest.php index 7e21ec2..2624923 100644 --- a/core/modules/views/lib/Drupal/views/Tests/Handler/FieldUnitTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/Handler/FieldUnitTest.php @@ -85,16 +85,22 @@ public function testQuery() { } /** - * Assertion helper which checks whether a string is part of another string. + * Asserts that a string is part of another string. * * @param string $haystack * The value to search in. * @param string $needle * The value to search for. * @param string $message - * The message to display along with the assertion. + * (optional) A message to display with the assertion. Do not translate + * messages: use format_string() to embed variables in the message text, not + * t(). If left blank, a default message will be displayed. * @param string $group - * The type of assertion - examples are "Browser", "PHP". + * (optional) The group this message is in, which is displayed in a column + * in test output. Use 'Debug' to indicate this is debugging output. Do not + * translate this string. Defaults to 'Other'; most tests do not override + * this default. + * * @return bool * TRUE if the assertion succeeded, FALSE otherwise. */ @@ -103,16 +109,22 @@ protected function assertSubString($haystack, $needle, $message = '', $group = ' } /** - * Assertion helper which checks whether a string is not part of another string. + * Asserts that a string is not part of another string. * * @param string $haystack * The value to search in. * @param string $needle * The value to search for. * @param string $message - * The message to display along with the assertion. + * (optional) A message to display with the assertion. Do not translate + * messages: use format_string() to embed variables in the message text, not + * t(). If left blank, a default message will be displayed. * @param string $group - * The type of assertion - examples are "Browser", "PHP". + * (optional) The group this message is in, which is displayed in a column + * in test output. Use 'Debug' to indicate this is debugging output. Do not + * translate this string. Defaults to 'Other'; most tests do not override + * this default. + * * @return bool * TRUE if the assertion succeeded, FALSE otherwise. */ diff --git a/core/modules/views/lib/Drupal/views/Tests/ViewTestBase.php b/core/modules/views/lib/Drupal/views/Tests/ViewTestBase.php index 02c86f5..d477524 100644 --- a/core/modules/views/lib/Drupal/views/Tests/ViewTestBase.php +++ b/core/modules/views/lib/Drupal/views/Tests/ViewTestBase.php @@ -1,7 +1,8 @@ assertIdenticalResultsetHelper($view, $expected_result, $column_map, $message, 'assertIdentical'); } /** - * Helper function: verify a result set returned by view.. + * Verifies that a result set returned by a View differs from certain values. * * Inverse of ViewsTestCase::assertIdenticalResultset(). * - * @param $view - * An executed View. - * @param $expected_result - * An expected result set. - * @param $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 \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') { + protected function assertNotIdenticalResultset($view, $expected_result, $column_map = array(), $message = 'Non-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(); @@ -130,7 +175,19 @@ protected function assertIdenticalResultsetHelper($view, $expected_result, $colu } /** - * Helper function: order an array of array based on a column. + * 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; @@ -144,17 +201,30 @@ protected function orderResultSet($result_set, $column, $reverse = FALSE) { } /** - * Helper function to check whether a button with a certain id exists and has a certain label. + * Asserts the existence of a button with a certain ID and label. + * + * @param string $id + * The HTML ID of the button + * @param string $label. + * The expected label for the button. + * @param string $message + * (optional) A custom message to display with the assertion. If no custom + * message is provided, the message will indicate the button label. + * + * @return bool + * TRUE if the asserion was succesful, or FALSE on failure. */ protected function helperButtonHasLabel($id, $expected_label, $message = 'Label has the expected value: %label.') { return $this->assertFieldById($id, $expected_label, t($message, array('%label' => $expected_label))); } /** - * Helper function to execute a view with debugging. + * Executes a view with debugging. * - * @param view $view + * @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(); @@ -164,37 +234,38 @@ protected function executeView($view, $args = array()) { } /** - * The schema definition. + * Returns the schema definition. */ protected function schemaDefinition() { return ViewTestData::schemaDefinition(); } /** - * The views data definition. + * Returns the views data definition. */ protected function viewsData() { return ViewTestData::viewsData(); } /** - * A very simple test dataset. + * Returns a very simple test dataset. */ protected function dataSet() { return ViewTestData::dataSet(); } /** - * Build and return a basic view of the views_test_data table. + * Builds and returns a basic view of the views_test_data table. * * @return Drupal\views\ViewExecutable + * The built view object. */ protected function getBasicView() { return $this->createViewFromConfig('test_view'); } /** - * Creates a new View instance by creating directly from config data. + * Creates a new View instance by creating it directly from config data. * * @param string $view_name * The name of the test view to create. diff --git a/core/modules/views/lib/Drupal/views/Tests/ViewTestData.php b/core/modules/views/lib/Drupal/views/Tests/ViewTestData.php index efd9f5a..6a2d87c 100644 --- a/core/modules/views/lib/Drupal/views/Tests/ViewTestData.php +++ b/core/modules/views/lib/Drupal/views/Tests/ViewTestData.php @@ -8,7 +8,7 @@ namespace Drupal\views\Tests; /** - * A class which contains the base schema, example and views data for tests. + * Provides tests view data and the base test schema with sample data records. * * The methods will be used by both views test base classes. * @@ -18,7 +18,7 @@ class ViewTestData { /** - * The schema definition. + * Returns the schema definition. */ public static function schemaDefinition() { $schema['views_test_data'] = array( @@ -69,7 +69,7 @@ public static function schemaDefinition() { } /** - * The views data definition. + * Returns the views data definition. */ public static function viewsData() { // Declaration of the base table. @@ -172,7 +172,7 @@ public static function viewsData() { } /** - * A very simple test dataset. + * Returns a very simple test dataset. */ public static function dataSet() { return array( @@ -208,5 +208,6 @@ public static function dataSet() { ), ); } + } diff --git a/core/modules/views/lib/Drupal/views/Tests/ViewUnitTestBase.php b/core/modules/views/lib/Drupal/views/Tests/ViewUnitTestBase.php index 0151123..6cec8db 100644 --- a/core/modules/views/lib/Drupal/views/Tests/ViewUnitTestBase.php +++ b/core/modules/views/lib/Drupal/views/Tests/ViewUnitTestBase.php @@ -13,7 +13,14 @@ use Symfony\Component\HttpFoundation\Request; /** - * Abstract class for views testing. + * 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. + * + * @see \Drupal\views\Tests\ViewTestBase + * @see \Drupal\simpletest\DrupalUnitTestBase */ abstract class ViewUnitTestBase extends DrupalUnitTestBase { @@ -36,42 +43,80 @@ protected function setUp() { $query->execute(); } + /** - * Helper function: verify a result set returned by view. + * 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 $view - * An executed View. - * @param $expected_result - * An expected result set. - * @param $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 \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'); } /** - * Helper function: verify a result set returned by view.. + * Verifies that a result set returned by a View differs from certain values. * * Inverse of ViewsTestCase::assertIdenticalResultset(). * - * @param $view - * An executed View. - * @param $expected_result - * An expected result set. - * @param $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 \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(); @@ -105,7 +150,19 @@ protected function assertIdenticalResultsetHelper($view, $expected_result, $colu } /** - * Helper function: order an array of array based on a column. + * 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; @@ -119,10 +176,12 @@ protected function orderResultSet($result_set, $column, $reverse = FALSE) { } /** - * Helper function to execute a view with debugging. + * Executes a view with debugging. * - * @param view $view + * @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(); @@ -132,21 +191,21 @@ protected function executeView($view, $args = array()) { } /** - * The schema definition. + * Returns the schema definition. */ protected function schemaDefinition() { return ViewTestData::schemaDefinition(); } /** - * The views data definition. + * Returns the views data definition. */ protected function viewsData() { return ViewTestData::viewsData(); } /** - * A very simple test dataset. + * Returns a very simple test dataset. */ protected function dataSet() { return ViewTestData::dataSet();