annotate core/modules/dblog/tests/src/Functional/DbLogResourceTest.php @ 19:fa3358dc1485 tip

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