DBLogTest.php 1.95 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?php

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

namespace Drupal\rest\Tests;

use Drupal\rest\Tests\RESTTestBase;

/**
 * Tests the Watchdog resource to retrieve log messages.
 */
class DBLogTest extends RESTTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
22
  public static $modules = array('jsonld', 'rest', 'dblog');
23 24 25 26 27 28 29 30 31 32 33 34

  public static function getInfo() {
    return array(
      'name' => 'DB Log resource',
      'description' => 'Tests the watchdog database log resource.',
      'group' => 'REST',
    );
  }

  public function setUp() {
    parent::setUp();
    // Enable web API for the watchdog resource.
35
    $this->enableService('dblog');
36 37 38 39 40 41 42 43
  }

  /**
   * Writes a log messages and retrieves it via the web API.
   */
  public function testWatchdog() {
    // Write a log message to the DB.
    watchdog('rest_test', 'Test message');
44 45 46
    // Get the ID of the written message.
    $id = db_query_range("SELECT wid FROM {watchdog} WHERE type = :type ORDER BY wid DESC", 0, 1, array(':type' => 'rest_test'))
      ->fetchField();
47 48 49 50 51 52

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

53
    $response = $this->httpRequest("dblog/$id", 'GET', NULL, 'application/json');
54
    $this->assertResponse(200);
55
    $this->assertHeader('Content-Type', 'application/json');
56 57 58 59 60 61
    $log = drupal_json_decode($response);
    $this->assertEqual($log['wid'], $id, 'Log ID is correct.');
    $this->assertEqual($log['type'], 'rest_test', 'Type of log message is correct.');
    $this->assertEqual($log['message'], 'Test message', 'Log message text is correct.');

    // Request an unknown log entry.
62
    $response = $this->httpRequest("dblog/9999", 'GET', NULL, 'application/json');
63 64 65 66
    $this->assertResponse(404);
    $this->assertEqual($response, 'Not Found', 'Response message is correct.');
  }
}