Chris@0: enableService('dblog'); 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@0: $id = db_query_range("SELECT wid FROM {watchdog} WHERE type = :type ORDER BY wid DESC", 0, 1, [':type' => 'rest']) Chris@0: ->fetchField(); 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: $account = $this->drupalCreateUser(['restful get dblog']); Chris@0: $this->drupalLogin($account); Chris@0: Chris@0: $response = $this->httpRequest(Url::fromRoute('rest.dblog.GET.' . $this->defaultFormat, ['id' => $id, '_format' => $this->defaultFormat]), 'GET'); Chris@0: $this->assertResponse(200); Chris@0: $this->assertHeader('content-type', $this->defaultMimeType); Chris@0: $log = Json::decode($response); 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: $response = $this->httpRequest(Url::fromRoute('rest.dblog.GET.' . $this->defaultFormat, ['id' => 9999, '_format' => $this->defaultFormat]), 'GET'); Chris@0: $this->assertResponse(404); Chris@0: $decoded = Json::decode($response); Chris@0: $this->assertEqual($decoded['message'], 'Log entry with ID 9999 was not found', 'Response message is correct.'); Chris@0: Chris@0: // Make a bad request (a true malformed request would never be a route match). Chris@0: $response = $this->httpRequest(Url::fromRoute('rest.dblog.GET.' . $this->defaultFormat, ['id' => 0, '_format' => $this->defaultFormat]), 'GET'); Chris@0: $this->assertResponse(400); Chris@0: $decoded = Json::decode($response); Chris@0: $this->assertEqual($decoded['message'], 'No log entry ID was provided', 'Response message is correct.'); Chris@0: } Chris@0: Chris@0: }