3 namespace Drupal\Tests\dblog\Functional;
5 use Drupal\Component\Serialization\Json;
7 use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
8 use Drupal\Tests\rest\Functional\ResourceTestBase;
11 * Tests the watchdog database log resource.
15 class DbLogResourceTest extends ResourceTestBase {
17 use CookieResourceTestTrait;
22 protected static $format = 'hal_json';
27 protected static $mimeType = 'application/hal+json';
32 protected static $auth = 'cookie';
37 protected static $resourceConfigId = 'dblog';
42 public static $modules = ['hal', 'dblog'];
47 public function setUp() {
50 $auth = isset(static::$auth) ? [static::$auth] : [];
51 $this->provisionResource([static::$format], $auth);
55 * Writes a log messages and retrieves it via the REST API.
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'])
64 $this->initAuthentication();
65 $url = Url::fromRoute('rest.dblog.GET.' . static::$format, ['id' => $id, '_format' => static::$format]);
66 $request_options = $this->getAuthenticationRequestOptions('GET');
68 $response = $this->request('GET', $url, $request_options);
69 $this->assertResourceErrorResponse(403, "The 'restful get dblog' permission is required.", $response);
71 // Create a user account that has the required permissions to read
72 // the watchdog resource via the REST API.
73 $this->setUpAuthorization('GET');
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.');
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);
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);
96 protected function setUpAuthorization($method) {
99 $this->grantPermissionsToTestedRole(['restful get dblog']);
103 throw new \UnexpectedValueException();
110 protected function assertNormalizationEdgeCases($method, Url $url, array $request_options) {}
115 protected function getExpectedUnauthorizedAccessMessage($method) {}
120 protected function getExpectedBcUnauthorizedAccessMessage($method) {}
125 protected function getExpectedUnauthorizedAccessCacheability() {}