annotate core/modules/dblog/tests/src/Functional/DbLogResourceTest.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\dblog\Functional;
Chris@0 4
Chris@0 5 use Drupal\Component\Serialization\Json;
Chris@5 6 use Drupal\Core\Database\Database;
Chris@0 7 use Drupal\Core\Url;
Chris@0 8 use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
Chris@0 9 use Drupal\Tests\rest\Functional\ResourceTestBase;
Chris@0 10
Chris@0 11 /**
Chris@0 12 * Tests the watchdog database log resource.
Chris@0 13 *
Chris@0 14 * @group dblog
Chris@0 15 */
Chris@0 16 class DbLogResourceTest extends ResourceTestBase {
Chris@0 17
Chris@0 18 use CookieResourceTestTrait;
Chris@0 19
Chris@0 20 /**
Chris@0 21 * {@inheritdoc}
Chris@0 22 */
Chris@0 23 protected static $format = 'hal_json';
Chris@0 24
Chris@0 25 /**
Chris@0 26 * {@inheritdoc}
Chris@0 27 */
Chris@0 28 protected static $mimeType = 'application/hal+json';
Chris@0 29
Chris@0 30 /**
Chris@0 31 * {@inheritdoc}
Chris@0 32 */
Chris@0 33 protected static $auth = 'cookie';
Chris@0 34
Chris@0 35 /**
Chris@0 36 * {@inheritdoc}
Chris@0 37 */
Chris@0 38 protected static $resourceConfigId = 'dblog';
Chris@0 39
Chris@0 40 /**
Chris@0 41 * {@inheritdoc}
Chris@0 42 */
Chris@0 43 public static $modules = ['hal', 'dblog'];
Chris@0 44
Chris@0 45 /**
Chris@0 46 * {@inheritdoc}
Chris@0 47 */
Chris@0 48 public function setUp() {
Chris@0 49 parent::setUp();
Chris@0 50
Chris@0 51 $auth = isset(static::$auth) ? [static::$auth] : [];
Chris@0 52 $this->provisionResource([static::$format], $auth);
Chris@0 53 }
Chris@0 54
Chris@0 55 /**
Chris@0 56 * Writes a log messages and retrieves it via the REST API.
Chris@0 57 */
Chris@0 58 public function testWatchdog() {
Chris@0 59 // Write a log message to the DB.
Chris@0 60 $this->container->get('logger.channel.rest')->notice('Test message');
Chris@0 61 // Get the ID of the written message.
Chris@5 62 $id = Database::getConnection()->queryRange("SELECT wid FROM {watchdog} WHERE type = :type ORDER BY wid DESC", 0, 1, [':type' => 'rest'])
Chris@0 63 ->fetchField();
Chris@0 64
Chris@0 65 $this->initAuthentication();
Chris@0 66 $url = Url::fromRoute('rest.dblog.GET', ['id' => $id, '_format' => static::$format]);
Chris@0 67 $request_options = $this->getAuthenticationRequestOptions('GET');
Chris@0 68
Chris@0 69 $response = $this->request('GET', $url, $request_options);
Chris@4 70 $this->assertResourceErrorResponse(403, "The 'restful get dblog' permission is required.", $response, ['4xx-response', 'http_response'], ['user.permissions'], FALSE, FALSE);
Chris@0 71
Chris@0 72 // Create a user account that has the required permissions to read
Chris@0 73 // the watchdog resource via the REST API.
Chris@0 74 $this->setUpAuthorization('GET');
Chris@0 75
Chris@0 76 $response = $this->request('GET', $url, $request_options);
Chris@0 77 $this->assertResourceResponse(200, FALSE, $response, ['config:rest.resource.dblog', 'config:rest.settings', 'http_response'], ['user.permissions'], FALSE, 'MISS');
Chris@0 78 $log = Json::decode((string) $response->getBody());
Chris@0 79 $this->assertEqual($log['wid'], $id, 'Log ID is correct.');
Chris@0 80 $this->assertEqual($log['type'], 'rest', 'Type of log message is correct.');
Chris@0 81 $this->assertEqual($log['message'], 'Test message', 'Log message text is correct.');
Chris@0 82
Chris@0 83 // Request an unknown log entry.
Chris@0 84 $url->setRouteParameter('id', 9999);
Chris@0 85 $response = $this->request('GET', $url, $request_options);
Chris@0 86 $this->assertResourceErrorResponse(404, 'Log entry with ID 9999 was not found', $response);
Chris@0 87
Chris@0 88 // Make a bad request (a true malformed request would never be a route match).
Chris@0 89 $url->setRouteParameter('id', 0);
Chris@0 90 $response = $this->request('GET', $url, $request_options);
Chris@0 91 $this->assertResourceErrorResponse(400, 'No log entry ID was provided', $response);
Chris@0 92 }
Chris@0 93
Chris@0 94 /**
Chris@0 95 * {@inheritdoc}
Chris@0 96 */
Chris@0 97 protected function setUpAuthorization($method) {
Chris@0 98 switch ($method) {
Chris@0 99 case 'GET':
Chris@0 100 $this->grantPermissionsToTestedRole(['restful get dblog']);
Chris@0 101 break;
Chris@0 102
Chris@0 103 default:
Chris@0 104 throw new \UnexpectedValueException();
Chris@0 105 }
Chris@0 106 }
Chris@0 107
Chris@0 108 /**
Chris@0 109 * {@inheritdoc}
Chris@0 110 */
Chris@0 111 protected function assertNormalizationEdgeCases($method, Url $url, array $request_options) {}
Chris@0 112
Chris@0 113 /**
Chris@0 114 * {@inheritdoc}
Chris@0 115 */
Chris@0 116 protected function getExpectedUnauthorizedAccessMessage($method) {}
Chris@0 117
Chris@0 118 /**
Chris@0 119 * {@inheritdoc}
Chris@0 120 */
Chris@0 121 protected function getExpectedBcUnauthorizedAccessMessage($method) {}
Chris@0 122
Chris@0 123 /**
Chris@0 124 * {@inheritdoc}
Chris@0 125 */
Chris@0 126 protected function getExpectedUnauthorizedAccessCacheability() {}
Chris@0 127
Chris@0 128 }