Comments

geertvd created an issue. See original summary.

geertvd’s picture

Issue summary: View changes
Status: Postponed » Active
dawehner’s picture

I'm pretty sure you would run into #2553661: KernelTestBase fails to set up FileCache

dawehner’s picture

Please keep in mind the following quote, see https://www.drupal.org/node/2489956

Conversion
Note: Until Drupal 8.0.x just straight conversions are allowed to land in core itself. What are straight conversions. Straight conversions are conversions
that just change the base class

geertvd’s picture

Status: Active » Needs review
StatusFileSize
new8.69 KB

This was pretty straightforward.

I ported 2 existing tests as an example (CommentUserNameTest and RenderCacheIntegrationTest), these are passing locally for me.
I guess those ported tests should be removed from this patch before it get's committed as porting those existing tests should be done in another ticket.

I did have to apply #2553661: KernelTestBase fails to set up FileCache so this is still included in this patch.

geertvd’s picture

+++ b/core/modules/views/tests/src/Kernel/ViewKernelTestBase.php
@@ -0,0 +1,152 @@
+/**
+ * @file
+ * Contains \Drupal\views\Tests\ViewKernelTestBase.
+ */

I need to fix that file block in the next patch.

Status: Needs review » Needs work

The last submitted patch, 5: port_viewkerneltestbase-2556855-5.patch, failed testing.

geertvd’s picture

Status: Needs work » Needs review
StatusFileSize
new2 KB
new41.36 KB
dawehner’s picture

+++ b/core/modules/views/tests/src/Kernel/ViewKernelTestBase.php
@@ -0,0 +1,152 @@
+abstract class ViewKernelTestBase extends KernelTestBase {
+
+  use ViewResultAssertionTrait;
+
+  /**
...
+   *
+   * @var array
+   */
+  public static $modules = array('system', 'views', 'views_test_config', 'views_test_data', 'user');
+
+  /**
+   * {@inheritdoc}
+   *
+   * @param bool $import_test_views
+   *   Should the views specififed on the test class be imported. If you need
+   *   to setup some additional stuff, like fields, you need to call false and
+   *   then call createTestViews for your own.
+   */
+  protected function setUp($import_test_views = TRUE) {
+    parent::setUp();
+
+    $this->installSchema('system', array('router', 'sequences'));
+    $this->setUpFixtures();
+
+    if ($import_test_views) {
+      ViewTestData::createTestViews(get_class($this), array('views_test_config'));
+    }
+  }
+
+  /**
+   * Sets up the configuration and schema of views and views_test_data modules.
+   *
+   * Because the schema of views_test_data.module is dependent on the test
+   * using it, it cannot be enabled normally.
+   */
+  protected function setUpFixtures() {
+    // First install the system module. Many Views have Page displays have menu
+    // links, and for those to work, the system menus must already be present.
+    $this->installConfig(array('system'));
+
+    // Define the schema and views data variable before enabling the test module.
+    \Drupal::state()->set('views_test_data_schema', $this->schemaDefinition());
+    \Drupal::state()->set('views_test_data_views_data', $this->viewsData());
+
+    $this->installConfig(array('views', 'views_test_config', 'views_test_data'));
+    foreach ($this->schemaDefinition() as $table => $schema) {
+      $this->installSchema('views_test_data', $table);
+    }
+
+    \Drupal::service('router.builder')->rebuild();
+
+    // 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();
+  }
+
+  /**
+   * 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, array $args = array()) {
+    $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);
+  }
+
+  /**
+   * 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();
+  }

I'm curious whether we could move all that code from ViewKernelTestBase into a trait and then have a happy world in which we can share the code ...

geertvd’s picture

Something like this then.

dawehner’s picture

Oh that looks much much better!

+++ b/core/modules/views/tests/src/Kernel/ViewKernelTestTrait.php
@@ -0,0 +1,138 @@
+  protected function setUp($import_test_views = TRUE) {
+    parent::setUp();

I'm not entirely sure whether the trait should implement setup(), but for now this seems okay.

geertvd’s picture

Status: Needs review » Postponed

Yea, I thought about not adding the setUp() method to the trait, but I thought the same could be said about setUpFixtures.

For now I think this is postponed until #2553661: KernelTestBase fails to set up FileCache is in.

geertvd’s picture

Status: Postponed » Needs review
StatusFileSize
new10.72 KB

#2553661: KernelTestBase fails to set up FileCache is in now so we can continue with this.

I ported \Drupal\views\Tests\BasicTest to use the new ViewKernelTestBase to include some form of test coverage for this patch.

dawehner’s picture

I really like the patch! I mean it enables a better future but I'm not sure whether this can land at this point.

heykarthikwithu’s picture

Issue tags: +Needs reroll

patch no longer applies.

dawehner’s picture

Status: Needs review » Fixed

We no longer need this issue

Status: Fixed » Needs work

The last submitted patch, 13: port_viewkerneltestbase-2556855-13.patch, failed testing.

dawehner’s picture

Status: Needs work » Fixed

Meh

dbjpanda’s picture

StatusFileSize
new22.52 KB

error
While trying to reroll this patch for 8.2.x, i got to know that ViewKernelTestBase class has been removed in 8.2.x but still its there. I am a newbie. Can any one please help me to clear this confusion.

dawehner’s picture

@dbjpanda Don't worry about this issue. Its fixed. Some flakyness on d.o. reopened the ticket though

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.