Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / system / src / Tests / Session / SessionAuthenticationTest.php
1 <?php
2
3 namespace Drupal\system\Tests\Session;
4
5 use Drupal\Core\Url;
6 use Drupal\basic_auth\Tests\BasicAuthTestTrait;
7 use Drupal\simpletest\WebTestBase;
8
9 /**
10  * Tests if sessions are correctly handled when a user authenticates.
11  *
12  * @group Session
13  */
14 class SessionAuthenticationTest extends WebTestBase {
15
16   use BasicAuthTestTrait;
17
18   /**
19    * A test user.
20    *
21    * @var \Drupal\user\Entity\User
22    */
23   protected $user;
24
25   /**
26    * {@inheritdoc}
27    */
28   public static $modules = ['basic_auth', 'session_test'];
29
30   /**
31    * {@inheritdoc}
32    */
33   protected function setUp() {
34     parent::setUp();
35
36     // Create a test administrator user.
37     $this->user = $this->drupalCreateUser(['administer site configuration']);
38   }
39
40   /**
41    * Check that a basic authentication session does not leak.
42    *
43    * Regression test for a bug that caused a session initiated by basic
44    * authentication to persist over subsequent unauthorized requests.
45    */
46   public function testSessionFromBasicAuthenticationDoesNotLeak() {
47     // This route is authorized through basic_auth only, not cookie.
48     $protected_url = Url::fromRoute('session_test.get_session_basic_auth');
49
50     // This route is not protected.
51     $unprotected_url = Url::fromRoute('session_test.get_session_no_auth');
52
53     // Test that the route is not accessible as an anonymous user.
54     $this->drupalGet($protected_url);
55     $this->assertResponse(401, 'An anonymous user cannot access a route protected with basic authentication.');
56
57     // We should be able to access the route with basic authentication.
58     $this->basicAuthGet($protected_url, $this->user->getUsername(), $this->user->pass_raw);
59     $this->assertResponse(200, 'A route protected with basic authentication can be accessed by an authenticated user.');
60
61     // Check that the correct user is logged in.
62     $this->assertEqual($this->user->id(), json_decode($this->getRawContent())->user, 'The correct user is authenticated on a route with basic authentication.');
63
64     // If we now try to access a page without basic authentication then we
65     // should no longer be logged in.
66     $this->drupalGet($unprotected_url);
67     $this->assertResponse(200, 'An unprotected route can be accessed without basic authentication.');
68     $this->assertFalse(json_decode($this->getRawContent())->user, 'The user is no longer authenticated after visiting a page without basic authentication.');
69
70     // If we access the protected page again without basic authentication we
71     // should get 401 Unauthorized.
72     $this->drupalGet($protected_url);
73     $this->assertResponse(401, 'A subsequent request to the same route without basic authentication is not authorized.');
74   }
75
76   /**
77    * Tests if a session can be initiated through basic authentication.
78    */
79   public function testBasicAuthSession() {
80     // Set a session value on a request through basic auth.
81     $test_value = 'alpaca';
82     $response = $this->basicAuthGet('session-test/set-session/' . $test_value, $this->user->getUsername(), $this->user->pass_raw);
83     $this->assertSessionData($response, $test_value);
84     $this->assertResponse(200, 'The request to set a session value was successful.');
85
86     // Test that on a subsequent request the session value is still present.
87     $response = $this->basicAuthGet('session-test/get-session', $this->user->getUsername(), $this->user->pass_raw);
88     $this->assertSessionData($response, $test_value);
89     $this->assertResponse(200, 'The request to get a session value was successful.');
90   }
91
92   /**
93    * Checks the session data returned by the session test routes.
94    *
95    * @param string $response
96    *   A response object containing the session values and the user ID.
97    * @param string $expected
98    *   The expected session value.
99    */
100   protected function assertSessionData($response, $expected) {
101     $response = json_decode($response, TRUE);
102     $this->assertEqual(['test_value' => $expected], $response['session'], 'The session data matches the expected value.');
103
104     // Check that we are logged in as the correct user.
105     $this->assertEqual($this->user->id(), $response['user'], 'The correct user is logged in.');
106   }
107
108   /**
109    * Tests that a session is not started automatically by basic authentication.
110    */
111   public function testBasicAuthNoSession() {
112     // A route that is authorized through basic_auth only, not cookie.
113     $no_cookie_url = Url::fromRoute('session_test.get_session_basic_auth');
114
115     // A route that is authorized with standard cookie authentication.
116     $cookie_url = '<front>';
117
118     // If we authenticate with a third party authentication system then no
119     // session cookie should be set, the third party system is responsible for
120     // sustaining the session.
121     $this->basicAuthGet($no_cookie_url, $this->user->getUsername(), $this->user->pass_raw);
122     $this->assertResponse(200, 'The user is successfully authenticated using basic authentication.');
123     $this->assertFalse($this->drupalGetHeader('set-cookie', TRUE), 'No cookie is set on a route protected with basic authentication.');
124
125     // On the other hand, authenticating using Cookie sets a cookie.
126     $edit = ['name' => $this->user->getUsername(), 'pass' => $this->user->pass_raw];
127     $this->drupalPostForm($cookie_url, $edit, t('Log in'));
128     $this->assertResponse(200, 'The user is successfully authenticated using cookie authentication.');
129     $this->assertTrue($this->drupalGetHeader('set-cookie', TRUE), 'A cookie is set on a route protected with cookie authentication.');
130   }
131
132 }