annotate core/modules/dblog/tests/src/Functional/DbLogResourceTest.php @ 4:a9cd425dd02b

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