1

I'm trying to write a test on a settings form. The module uses a queue, and the settings form displays the items in the queue like so:

enter image description here

When I extend the test class from WebTestBase I get:

Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mskcc_local_d8.test73723996queue' doesn't exist: SELECT COUNT(*) AS expression FROM (SELECT 1 AS expression FROM {queue} q WHERE name = :db_condition_placeholder_0) subquery; Array ( [:db_condition_placeholder_0] => google_analytics_counter_worker ) in Drupal\google_analytics_counter\GoogleAnalyticsCounterManager->getCount() (line 635 of modules/contrib/google_analytics_counter/src/GoogleAnalyticsCounterManager.php).
Drupal\Core\Database\Statement->execute(Array, Array) (Line: 625)
Drupal\Core\Database\Connection->query('SELECT COUNT(*) AS expression
FROM 
(SELECT 1 AS expression
FROM 
{queue} q
WHERE name = :db_condition_placeholder_0) subquery', Array, Array) (Line: 87)
Drupal\Core\Database\Driver\mysql\Connection->query('SELECT COUNT(*) AS expression
FROM 
(SELECT 1 AS expression
FROM 
{queue} q
WHERE name = :db_condition_placeholder_0) subquery', Array, Array) (Line: 510)
Drupal\Core\Database\Query\Select->execute() (Line: 635)
Drupal\google_analytics_counter\GoogleAnalyticsCounterManager->getCount('queue') (Line: 134)
Drupal\google_analytics_counter\Form\GoogleAnalyticsCounterSettingsForm->buildForm(Array, Object)
call_user_func_array(Array, Array) (Line: 514)
Drupal\Core\Form\FormBuilder->retrieveForm('google_analytics_counter_admin_settings', Object) (Line: 271)
Drupal\Core\Form\FormBuilder->buildForm('google_analytics_counter_admin_settings', Object) (Line: 74)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 582)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 151)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 664)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

which tells me that the queue table is missing from the automated test environment.

But when I extend the test class from KernelTestBase and add

// These additional tables are necessary because $this->cron->run() calls
// system_cron().
$this->installSchema('system', ['key_value_expire']);

$this->connection = Database::getConnection();
$this->cron = \Drupal::service('cron');

to setup(), I assume my automated testing environment will contain a queue table (still to be determined). But the methods I regularly use to test a form, like $this->drupalGet and $this->assertResponse will now be not found.

How can I test a settings form that displays the contents of the queue table?

Thanks

1 Answer 1

1

A functional test satisfies both requirements.

<?php

namespace Drupal\Tests\google_analytics_counter\Functional;

use Drupal\Component\Utility\SafeMarkup;
use Drupal\Tests\BrowserTestBase;

/**
 * Tests the google analytics counter settings form.
 *
 * @group statistics
 */
class GoogleAnalyticsCounterSettingsTest extends BrowserTestBase {
  const ADMIN_SETTINGS_PATH = 'admin/config/system/google-analytics-counter';

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = ['google_analytics_counter'];

  /**
   * A test user with administrative privileges.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $adminUser;

  /**
   * Verifies that the google analytics counter settings page works.
   *
   * @see MediaSourceTest for good example code.
   */
  public function testForm() {
    $admin_user = $this->drupalCreateUser(array(
      'administer site configuration',
      'administer google analytics counter',
    ));
    $this->drupalLogin($admin_user);

    // Create item(s) in the queue.
    $queue_name = 'google_analytics_counter_worker';
    $queue = \Drupal::queue($queue_name);

    for ($i = 1; $i <= 5; $i++) {
      $queue->createItem(['type' => 'fetch', 'index' => 'boogie' . $i]);
    }

    $this->drupalGet(self::ADMIN_SETTINGS_PATH);
    $this->assertResponse(200, 'Access granted to settings page.');

    // Assert Fields.
    $settings_fields = $this->getAdminUserSettingsFields();
    foreach ($settings_fields as $field_name) {
      $this->assertField($field_name, SafeMarkup::format('@field_name field exists.', ['@field_name' => $field_name]));
    }

    // Cron Settings.
    $edit = [
      'cron_interval' => 0,
      'chunk_to_fetch' => 5000,
      'api_dayquota' => 10000,
      'cache_length' => 24,
    ];

    // Enable counter on content view.
    $this->drupalPostForm(self::ADMIN_SETTINGS_PATH, $edit, t('Save configuration'));
    $this->assertRaw('The configuration options have been saved.');
  }

  /**
   * Returns a list containing the admin settings fields.
   */
  protected function getAdminUserSettingsFields() {
    return [
      'cron_interval',
      'chunk_to_fetch',
      'api_dayquota',
      'cache_length',
      'queue_time',
      'start_date',
      'advanced_date_checkbox',
      'fixed_start_date',
      'fixed_end_date',
    ];
  }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.