DbLogResourceTest.php 1.91 KB
Newer Older
1 2 3 4 5 6 7
<?php

/**
 * @file
 * Definition of Drupal\rest\test\DBLogTest.
 */

8
namespace Drupal\dblog\Tests\Rest;
9

10
use Drupal\Component\Serialization\Json;
11 12 13
use Drupal\rest\Tests\RESTTestBase;

/**
14 15 16
 * Tests the watchdog database log resource.
 *
 * @group dblog
17
 */
18
class DbLogResourceTest extends RESTTestBase {
19 20 21 22 23 24

  /**
   * Modules to enable.
   *
   * @var array
   */
25
  public static $modules = array('hal', 'dblog');
26

27
  protected function setUp() {
28
    parent::setUp();
29
    // Enable REST API for the watchdog resource.
30
    $this->enableService('dblog');
31 32 33
  }

  /**
34
   * Writes a log messages and retrieves it via the REST API.
35 36 37
   */
  public function testWatchdog() {
    // Write a log message to the DB.
38
    $this->container->get('logger.channel.rest')->notice('Test message');
39
    // Get the ID of the written message.
40
    $id = db_query_range("SELECT wid FROM {watchdog} WHERE type = :type ORDER BY wid DESC", 0, 1, array(':type' => 'rest'))
41
      ->fetchField();
42 43

    // Create a user account that has the required permissions to read
44
    // the watchdog resource via the REST API.
45 46 47
    $account = $this->drupalCreateUser(array('restful get dblog'));
    $this->drupalLogin($account);

48
    $response = $this->httpRequest("dblog/$id", 'GET', NULL, $this->defaultMimeType);
49
    $this->assertResponse(200);
50
    $this->assertHeader('content-type', $this->defaultMimeType);
51
    $log = Json::decode($response);
52
    $this->assertEqual($log['wid'], $id, 'Log ID is correct.');
53
    $this->assertEqual($log['type'], 'rest', 'Type of log message is correct.');
54 55 56
    $this->assertEqual($log['message'], 'Test message', 'Log message text is correct.');

    // Request an unknown log entry.
57
    $response = $this->httpRequest("dblog/9999", 'GET', NULL, $this->defaultMimeType);
58
    $this->assertResponse(404);
59
    $decoded = Json::decode($response);
60
    $this->assertEqual($decoded['error'], 'Log entry with ID 9999 was not found', 'Response message is correct.');
61 62
  }
}