3 namespace Drupal\dblog\Tests\Rest;
5 use Drupal\Component\Serialization\Json;
7 use Drupal\rest\Tests\RESTTestBase;
10 * Tests the watchdog database log resource.
14 class DbLogResourceTest extends RESTTestBase {
21 public static $modules = ['hal', 'dblog'];
23 protected function setUp() {
25 // Enable REST API for the watchdog resource.
26 $this->enableService('dblog');
30 * Writes a log messages and retrieves it via the REST API.
32 public function testWatchdog() {
33 // Write a log message to the DB.
34 $this->container->get('logger.channel.rest')->notice('Test message');
35 // Get the ID of the written message.
36 $id = db_query_range("SELECT wid FROM {watchdog} WHERE type = :type ORDER BY wid DESC", 0, 1, [':type' => 'rest'])
39 // Create a user account that has the required permissions to read
40 // the watchdog resource via the REST API.
41 $account = $this->drupalCreateUser(['restful get dblog']);
42 $this->drupalLogin($account);
44 $response = $this->httpRequest(Url::fromRoute('rest.dblog.GET.' . $this->defaultFormat, ['id' => $id, '_format' => $this->defaultFormat]), 'GET');
45 $this->assertResponse(200);
46 $this->assertHeader('content-type', $this->defaultMimeType);
47 $log = Json::decode($response);
48 $this->assertEqual($log['wid'], $id, 'Log ID is correct.');
49 $this->assertEqual($log['type'], 'rest', 'Type of log message is correct.');
50 $this->assertEqual($log['message'], 'Test message', 'Log message text is correct.');
52 // Request an unknown log entry.
53 $response = $this->httpRequest(Url::fromRoute('rest.dblog.GET.' . $this->defaultFormat, ['id' => 9999, '_format' => $this->defaultFormat]), 'GET');
54 $this->assertResponse(404);
55 $decoded = Json::decode($response);
56 $this->assertEqual($decoded['message'], 'Log entry with ID 9999 was not found', 'Response message is correct.');
58 // Make a bad request (a true malformed request would never be a route match).
59 $response = $this->httpRequest(Url::fromRoute('rest.dblog.GET.' . $this->defaultFormat, ['id' => 0, '_format' => $this->defaultFormat]), 'GET');
60 $this->assertResponse(400);
61 $decoded = Json::decode($response);
62 $this->assertEqual($decoded['message'], 'No log entry ID was provided', 'Response message is correct.');