8815bf340f33ddf6d63073ff3fa13d8003b3a8f5
[yaffs-website] / web / core / modules / system / src / Tests / Cache / SessionExistsCacheContextTest.php
1 <?php
2
3 namespace Drupal\system\Tests\Cache;
4
5 use Drupal\Core\Url;
6 use Drupal\simpletest\WebTestBase;
7
8 /**
9  * Tests the 'session.exists' cache context service.
10  *
11  * @group Cache
12  */
13 class SessionExistsCacheContextTest extends WebTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['session_exists_cache_context_test'];
21
22   /**
23    * Tests \Drupal\Core\Cache\Context\SessionExistsCacheContext::getContext().
24    */
25   public function testCacheContext() {
26     $this->dumpHeaders = TRUE;
27
28     // 1. No session (anonymous).
29     $this->assertSessionCookieOnClient(FALSE);
30     $this->drupalGet(Url::fromRoute('<front>'));
31     $this->assertSessionCookieOnClient(FALSE);
32     $this->assertRaw('Session does not exist!');
33     $this->assertRaw('[session.exists]=0');
34
35     // 2. Session (authenticated).
36     $this->assertSessionCookieOnClient(FALSE);
37     $this->drupalLogin($this->rootUser);
38     $this->assertSessionCookieOnClient(TRUE);
39     $this->assertRaw('Session exists!');
40     $this->assertRaw('[session.exists]=1');
41     $this->drupalLogout();
42     $this->assertSessionCookieOnClient(FALSE);
43     $this->assertRaw('Session does not exist!');
44     $this->assertRaw('[session.exists]=0');
45
46     // 3. Session (anonymous).
47     $this->assertSessionCookieOnClient(FALSE);
48     $this->drupalGet(Url::fromRoute('<front>', [], ['query' => ['trigger_session' => 1]]));
49     $this->assertSessionCookieOnClient(TRUE);
50     $this->assertRaw('Session does not exist!');
51     $this->assertRaw('[session.exists]=0');
52     $this->drupalGet(Url::fromRoute('<front>'));
53     $this->assertSessionCookieOnClient(TRUE);
54     $this->assertRaw('Session exists!');
55     $this->assertRaw('[session.exists]=1');
56   }
57
58   /**
59    * Asserts whether a session cookie is present on the client or not.
60    */
61   public function assertSessionCookieOnClient($expected_present) {
62     $non_deleted_cookies = array_filter($this->cookies, function ($item) { return $item['value'] !== 'deleted'; });
63     $this->assertEqual($expected_present, isset($non_deleted_cookies[$this->getSessionName()]), 'Session cookie exists.');
64   }
65
66 }