Security update to Drupal 8.4.6
[yaffs-website] / web / core / modules / dblog / tests / src / Functional / DbLogResourceTest.php
1 <?php
2
3 namespace Drupal\Tests\dblog\Functional;
4
5 use Drupal\Component\Serialization\Json;
6 use Drupal\Core\Url;
7 use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
8 use Drupal\Tests\rest\Functional\ResourceTestBase;
9
10 /**
11  * Tests the watchdog database log resource.
12  *
13  * @group dblog
14  */
15 class DbLogResourceTest extends ResourceTestBase {
16
17   use CookieResourceTestTrait;
18
19   /**
20    * {@inheritdoc}
21    */
22   protected static $format = 'hal_json';
23
24   /**
25    * {@inheritdoc}
26    */
27   protected static $mimeType = 'application/hal+json';
28
29   /**
30    * {@inheritdoc}
31    */
32   protected static $auth = 'cookie';
33
34   /**
35    * {@inheritdoc}
36    */
37   protected static $resourceConfigId = 'dblog';
38
39   /**
40    * {@inheritdoc}
41    */
42   public static $modules = ['hal', 'dblog'];
43
44   /**
45    * {@inheritdoc}
46    */
47   public function setUp() {
48     parent::setUp();
49
50     $auth = isset(static::$auth) ? [static::$auth] : [];
51     $this->provisionResource([static::$format], $auth);
52   }
53
54   /**
55    * Writes a log messages and retrieves it via the REST API.
56    */
57   public function testWatchdog() {
58     // Write a log message to the DB.
59     $this->container->get('logger.channel.rest')->notice('Test message');
60     // Get the ID of the written message.
61     $id = db_query_range("SELECT wid FROM {watchdog} WHERE type = :type ORDER BY wid DESC", 0, 1, [':type' => 'rest'])
62       ->fetchField();
63
64     $this->initAuthentication();
65     $url = Url::fromRoute('rest.dblog.GET.' . static::$format, ['id' => $id, '_format' => static::$format]);
66     $request_options = $this->getAuthenticationRequestOptions('GET');
67
68     $response = $this->request('GET', $url, $request_options);
69     $this->assertResourceErrorResponse(403, "The 'restful get dblog' permission is required.", $response);
70
71     // Create a user account that has the required permissions to read
72     // the watchdog resource via the REST API.
73     $this->setUpAuthorization('GET');
74
75     $response = $this->request('GET', $url, $request_options);
76     $this->assertResourceResponse(200, FALSE, $response, ['config:rest.resource.dblog', 'config:rest.settings', 'http_response'], ['user.permissions'], FALSE, 'MISS');
77     $log = Json::decode((string) $response->getBody());
78     $this->assertEqual($log['wid'], $id, 'Log ID is correct.');
79     $this->assertEqual($log['type'], 'rest', 'Type of log message is correct.');
80     $this->assertEqual($log['message'], 'Test message', 'Log message text is correct.');
81
82     // Request an unknown log entry.
83     $url->setRouteParameter('id', 9999);
84     $response = $this->request('GET', $url, $request_options);
85     $this->assertResourceErrorResponse(404, 'Log entry with ID 9999 was not found', $response);
86
87     // Make a bad request (a true malformed request would never be a route match).
88     $url->setRouteParameter('id', 0);
89     $response = $this->request('GET', $url, $request_options);
90     $this->assertResourceErrorResponse(400, 'No log entry ID was provided', $response);
91   }
92
93   /**
94    * {@inheritdoc}
95    */
96   protected function setUpAuthorization($method) {
97     switch ($method) {
98       case 'GET':
99         $this->grantPermissionsToTestedRole(['restful get dblog']);
100         break;
101
102       default:
103         throw new \UnexpectedValueException();
104     }
105   }
106
107   /**
108    * {@inheritdoc}
109    */
110   protected function assertNormalizationEdgeCases($method, Url $url, array $request_options) {}
111
112   /**
113    * {@inheritdoc}
114    */
115   protected function getExpectedUnauthorizedAccessMessage($method) {}
116
117   /**
118    * {@inheritdoc}
119    */
120   protected function getExpectedBcUnauthorizedAccessMessage($method) {}
121
122   /**
123    * {@inheritdoc}
124    */
125   protected function getExpectedUnauthorizedAccessCacheability() {}
126
127 }