comparison core/modules/dblog/src/Tests/Rest/DbLogResourceTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\dblog\Tests\Rest;
4
5 use Drupal\Component\Serialization\Json;
6 use Drupal\Core\Url;
7 use Drupal\rest\Tests\RESTTestBase;
8
9 /**
10 * Tests the watchdog database log resource.
11 *
12 * @group dblog
13 */
14 class DbLogResourceTest extends RESTTestBase {
15
16 /**
17 * Modules to enable.
18 *
19 * @var array
20 */
21 public static $modules = ['hal', 'dblog'];
22
23 protected function setUp() {
24 parent::setUp();
25 // Enable REST API for the watchdog resource.
26 $this->enableService('dblog');
27 }
28
29 /**
30 * Writes a log messages and retrieves it via the REST API.
31 */
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'])
37 ->fetchField();
38
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);
43
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.');
51
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.');
57
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.');
63 }
64
65 }