annotate core/modules/dblog/tests/src/Functional/DbLogResourceTest.php @ 12:7a779792577d

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