2

I have a session variable, that once set changes block output of the system branding block. Normally this variable is intercepted from the query string by an inbound path processor, however that is a different test. In this test I just want to make sure the output is as expected.

So I set up my test as follows:

class ConfigOverrideCacheTest extends MyLabelTestBase {

  protected function setUp() {
    parent::setUp();

    $this->tokenOwner= $this->drupalCreateUser(['my permission']);
    $this->drupalLogin($this->tokenOwner);
  }

  public function testNotoken() {
    $this->drupalGet('<front>');
    // Check site name is Drupal.
    $this->assertSession()->elementTextContains('css', '.block-system-branding-block', 'Drupal');
  }

  public function testToken() {
    // Set session variable.
    \Drupal::service('session')->set('my_session_key', 'value');

    $this->drupalGet('<front>');
    // Check site name is NOT Drupal.
    $this->assertSession()->elementTextNotContains('css', '.block-system-branding-block', 'Drupal');
  }

}

This second test keeps failing, and the output is the same as in the first test. Which I suspect to be because it is set in the testrunner session, rather than the actual testing session.

How so set (or synchronize) session variables from the testrunner to the site that gets tested?

1
+100

Referring SessionTest, there is a test-module (session_test) in place, which get's enabled:

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('session_test');

And then called by GET request to set the session value:

 $this->drupalGet('session-test/set/' . $value_1);

I guess this procedure makes it really independent from the runner ..

So - make your module with one callback in .routing.yml, setting your session value in your controller. Your method afterwards could look like this:

public function testToken() {
  // Set session variable.
  $this->drupalGet('session-set/my-session-key/value');

  $this->drupalGet('<front>');
  // Check site name is NOT Drupal.
  $this->assertSession()->elementTextNotContains('css', '.block-system-branding-block', 'Drupal');
}

And, don't forget to enable the created module ;)


For completness, here the (above linked) SessionTest routing.yml:

session_test.set:
  path: '/session-test/set/{test_value}'
  defaults:
    _title: 'Set session value'
    _controller: '\Drupal\session_test\Controller\SessionTestController::set'
  options:
    no_cache: TRUE
    converters:
      test_value: '\s+'
  requirements:
    _access: 'TRUE'

and Controller:

public function set($test_value) {
  $_SESSION['session_test_value'] = $test_value;

  return ['#markup' => $this->t('The current value of the stored session variable has been set to %val', array('%val' => $test_value))];
}
| improve this answer | |
  • Thanks I had not found that. I just implemented something similar as a last resort yesterday, though I would prefer to do this without the extra call... – Neograph734 Sep 5 '17 at 7:57
1

I am not sure if the storage you use to save the my_session_key attribute is shared between the testrunner and the site that gets tested, and that could explain why the test fails.

Why not test exactly the case of the inbound path processing, e.g.:

public function testToken() {
  $options = [
    'query' => [
      'param' => 'value',
    ]
  ];
  $this->drupalGet('<front>', $options);
  // Check site name is NOT Drupal.
  $this->assertSession()->elementTextNotContains('css', '.block-system-branding-block', 'Drupal');
}

Good luck in any case!

| improve this answer | |
  • I am not sure if the storage you use to save the my_session_key attribute is shared between the testrunner and the site that gets tested, and that could explain why the test fails. Currently I am pretty sure it is not ;-). I felt it would be cleaner to separate the inbound path processing from the response and use two different tests, but from the looks of it, I will have to make a call to the site one way or another... – Neograph734 Sep 5 '17 at 7:55
  • This method seemed to be stored in the router cache, leading to unexpected outputs. But thanks anyway! – Neograph734 Sep 9 '17 at 10:45

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.