Chris@0: provisionResource([static::$format], $auth); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Writes a log messages and retrieves it via the REST API. Chris@0: */ Chris@0: public function testWatchdog() { Chris@0: // Write a log message to the DB. Chris@0: $this->container->get('logger.channel.rest')->notice('Test message'); Chris@0: // Get the ID of the written message. Chris@5: $id = Database::getConnection()->queryRange("SELECT wid FROM {watchdog} WHERE type = :type ORDER BY wid DESC", 0, 1, [':type' => 'rest']) Chris@0: ->fetchField(); Chris@0: Chris@0: $this->initAuthentication(); Chris@0: $url = Url::fromRoute('rest.dblog.GET', ['id' => $id, '_format' => static::$format]); Chris@0: $request_options = $this->getAuthenticationRequestOptions('GET'); Chris@0: Chris@0: $response = $this->request('GET', $url, $request_options); Chris@4: $this->assertResourceErrorResponse(403, "The 'restful get dblog' permission is required.", $response, ['4xx-response', 'http_response'], ['user.permissions'], FALSE, FALSE); Chris@0: Chris@0: // Create a user account that has the required permissions to read Chris@0: // the watchdog resource via the REST API. Chris@0: $this->setUpAuthorization('GET'); Chris@0: Chris@0: $response = $this->request('GET', $url, $request_options); Chris@0: $this->assertResourceResponse(200, FALSE, $response, ['config:rest.resource.dblog', 'config:rest.settings', 'http_response'], ['user.permissions'], FALSE, 'MISS'); Chris@0: $log = Json::decode((string) $response->getBody()); Chris@0: $this->assertEqual($log['wid'], $id, 'Log ID is correct.'); Chris@0: $this->assertEqual($log['type'], 'rest', 'Type of log message is correct.'); Chris@0: $this->assertEqual($log['message'], 'Test message', 'Log message text is correct.'); Chris@0: Chris@0: // Request an unknown log entry. Chris@0: $url->setRouteParameter('id', 9999); Chris@0: $response = $this->request('GET', $url, $request_options); Chris@0: $this->assertResourceErrorResponse(404, 'Log entry with ID 9999 was not found', $response); Chris@0: Chris@0: // Make a bad request (a true malformed request would never be a route match). Chris@0: $url->setRouteParameter('id', 0); Chris@0: $response = $this->request('GET', $url, $request_options); Chris@0: $this->assertResourceErrorResponse(400, 'No log entry ID was provided', $response); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function setUpAuthorization($method) { Chris@0: switch ($method) { Chris@0: case 'GET': Chris@0: $this->grantPermissionsToTestedRole(['restful get dblog']); Chris@0: break; Chris@0: Chris@0: default: Chris@0: throw new \UnexpectedValueException(); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function assertNormalizationEdgeCases($method, Url $url, array $request_options) {} Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function getExpectedUnauthorizedAccessMessage($method) {} Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function getExpectedBcUnauthorizedAccessMessage($method) {} Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function getExpectedUnauthorizedAccessCacheability() {} Chris@0: Chris@0: }