Newer
Older
Dries Buytaert
committed
<?php
/**
* @file
* Definition of Drupal\rest\test\DBLogTest.
*/
namespace Drupal\rest\Tests;
Angie Byron
committed
use Drupal\Component\Serialization\Json;
Dries Buytaert
committed
use Drupal\rest\Tests\RESTTestBase;
/**
* Tests the Watchdog resource to retrieve log messages.
*/
class DBLogTest extends RESTTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('hal', 'rest', 'dblog');
Dries Buytaert
committed
public static function getInfo() {
return array(
'name' => 'DB Log resource',
'description' => 'Tests the watchdog database log resource.',
'group' => 'REST',
);
}
public function setUp() {
parent::setUp();
Angie Byron
committed
// Enable REST API for the watchdog resource.
Dries Buytaert
committed
$this->enableService('dblog');
Dries Buytaert
committed
}
/**
Angie Byron
committed
* Writes a log messages and retrieves it via the REST API.
Dries Buytaert
committed
*/
public function testWatchdog() {
// Write a log message to the DB.
Alex Pott
committed
watchdog('rest_test', 'Test message');
// Get the ID of the written message.
Alex Pott
committed
$id = db_query_range("SELECT wid FROM {watchdog} WHERE type = :type ORDER BY wid DESC", 0, 1, array(':type' => 'rest_test'))
->fetchField();
Dries Buytaert
committed
// Create a user account that has the required permissions to read
Angie Byron
committed
// the watchdog resource via the REST API.
Dries Buytaert
committed
$account = $this->drupalCreateUser(array('restful get dblog'));
$this->drupalLogin($account);
$response = $this->httpRequest("dblog/$id", 'GET', NULL, $this->defaultMimeType);
Dries Buytaert
committed
$this->assertResponse(200);
$this->assertHeader('content-type', $this->defaultMimeType);
$log = Json::decode($response);
Dries Buytaert
committed
$this->assertEqual($log['wid'], $id, 'Log ID is correct.');
Alex Pott
committed
$this->assertEqual($log['type'], 'rest_test', 'Type of log message is correct.');
Dries Buytaert
committed
$this->assertEqual($log['message'], 'Test message', 'Log message text is correct.');
// Request an unknown log entry.
$response = $this->httpRequest("dblog/9999", 'GET', NULL, $this->defaultMimeType);
Dries Buytaert
committed
$this->assertResponse(404);
$decoded = Json::decode($response);
Dries Buytaert
committed
$this->assertEqual($decoded['error'], 'Log entry with ID 9999 was not found', 'Response message is correct.');