View source
<?php
class ViewsContentCacheTest extends DrupalWebTestCase {
function getInfo() {
return array(
'name' => t('Views content cache segment tests'),
'description' => t('Segment handling tests for the Views content cache module.'),
'group' => t('Views content cache'),
);
}
function testStoryContentAdded() {
$cid = array(
'node' => array(
'story',
),
);
$this
->assertFalse(views_content_cache_update_get($cid, TRUE), t('Ensure that the no timestamp has been set against the story cache segment.'));
$timestamp_before = time();
sleep(1);
$this->node = $this
->drupalCreateNode(array(
'type' => 'story',
));
sleep(1);
$timestamp_after = time();
$cid = array(
'node' => array(
'story',
),
'node_only' => array(
'node_changed',
),
);
$updated = views_content_cache_update_get($cid, TRUE);
$result = $updated < $timestamp_after && $updated > $timestamp_before;
$this
->assertTrue($result, t('Ensure that the timestamp against the story cache segment is updated.'));
$timestamp_before = time();
sleep(1);
$this->node = $this
->drupalCreateNode(array(
'type' => 'story',
));
sleep(1);
$timestamp_after = time();
$cid = array(
'node' => array(
'story',
),
'node_only' => array(
'node_changed',
),
);
$updated = views_content_cache_update_get($cid, TRUE);
$result = $updated < $timestamp_after && $updated > $timestamp_before;
$this
->assertTrue($result, t('Ensure that the timestamp against the story cache segment is updated.'));
$this
->assertFalse(views_content_cache_update_get(array(
'node' => array(
'page',
),
), TRUE), t('Ensure that the no timestamp has been set against the page cache segment.'));
}
function testCommentAdded() {
$cid = array(
'comment' => array(
'changed',
),
);
$this
->assertFalse(views_content_cache_update_get($cid, TRUE), t('Ensure that the no timestamp has been set against the global comment cache segment.'));
$this->node = $this
->drupalCreateNode(array(
'type' => 'story',
));
$timestamp_before = time();
$this->comment = $this
->postComment($this->node, $this
->randomName(32));
$timestamp_after = time();
$cid = array(
'comment' => array(
'changed',
),
);
$updated = views_content_cache_update_get($cid, TRUE);
$result = $updated <= $timestamp_after && $updated >= $timestamp_before;
$this
->assertTrue($result, t('Ensure that the timestamp against the global comment cache segment is updated.'));
$cid = array(
'node' => array(
'story',
),
'comment' => array(
'changed',
),
);
$updated = views_content_cache_update_get($cid, TRUE);
$result = $updated <= $timestamp_after && $updated >= $timestamp_before;
$this
->assertTrue($result, t('Ensure that the timestamp against the story node comment cache segment is updated.'));
$cid = array(
'node' => array(
'story',
),
'node_only' => array(
'node_changed',
),
);
$updated = views_content_cache_update_get($cid, TRUE);
$this
->assertFalse($updated, t('Ensure that the timestamp against the story only node comment cache segment is not updated.'));
$cid = array(
'node' => array(
'page',
),
'comment' => array(
'changed',
),
);
$updated = views_content_cache_update_get($cid, TRUE);
$this
->assertFalse($updated, t('Ensure that the timestamp against the page node comment cache segment is not updated.'));
}
public function setUp() {
parent::setUp('views_content_cache', 'ctools', 'views', 'comment');
$this->privileged_user = $this
->drupalCreateUser(array(
'create story content',
'edit own story content',
));
$this
->drupalLogin($this->privileged_user);
}
function postComment($node, $comment, $subject = '', $contact = NULL) {
$edit = array();
$edit['comment'] = $comment;
$preview_mode = variable_get('comment_preview_' . $node->type, COMMENT_PREVIEW_REQUIRED);
$subject_mode = variable_get('comment_subject_field_' . $node->type, 1);
if ($node !== NULL) {
$this
->drupalGet('comment/reply/' . $node->nid);
}
if ($subject_mode == TRUE) {
$edit['subject'] = $subject;
}
if ($contact !== NULL && is_array($contact)) {
$edit += $contact;
}
switch ($preview_mode) {
case COMMENT_PREVIEW_REQUIRED:
$this
->assertNoFieldByName('op', t('Save'), t('Save button not found.'));
$this
->drupalPost(NULL, $edit, t('Preview'));
case COMMENT_PREVIEW_OPTIONAL:
$this
->assertFieldByName('op', t('Preview'), t('Preview button found.'));
$this
->assertFieldByName('op', t('Save'), t('Save button found.'));
$this
->drupalPost(NULL, $edit, t('Save'));
break;
}
$match = array();
preg_match('/#comment-([0-9]+)/', $this
->getURL(), $match);
if ($contact !== TRUE) {
if ($subject) {
$this
->assertText($subject, 'Comment subject posted.');
}
$this
->assertText($comment, 'Comment body posted.');
$this
->assertTrue(!empty($match) && !empty($match[1]), t('Comment id found.'));
}
if (isset($match[1])) {
return (object) array(
'id' => $match[1],
'subject' => $subject,
'comment' => $comment,
);
}
}
}
class ViewsContentCacheViewsPluginTest extends DrupalWebTestCase {
function getInfo() {
return array(
'name' => t('Views content cache views plugin'),
'description' => t('Tests if the caching plugin for views is working.'),
'group' => t('Views content cache'),
);
}
function testCachedViewStoryAdd() {
$first_story = $this
->drupalCreateNode(array(
'type' => 'story',
));
sleep(1);
$this
->drupalGet('views_content_cache/latest_stories', array(
'query' => array(
'step' => 'add_first',
),
));
$this
->assertText($first_story->title, t('Ensure first story is rendered by the view.'));
$this
->assertNoText('[Served output from views_content_cache]', t("Ensure the view didn't come from the cache."));
$this
->drupalGet('views_content_cache/latest_stories', array(
'query' => array(
'step' => 'get_first_cached',
),
));
$this
->assertText('[Served output from views_content_cache]', t('Ensure the view came from the cache.'));
$second_story = $this
->drupalCreateNode(array(
'type' => 'story',
));
sleep(1);
$this
->drupalGet('views_content_cache/latest_stories', array(
'query' => array(
'step' => 'add_second',
),
));
$this
->assertText($second_story->title, t('Ensure second story is rendered by the view.'));
$this
->assertNoText('[Served output from views_content_cache]', t("Ensure the view didn't come from the cache."));
$this
->drupalGet('views_content_cache/latest_stories', array(
'query' => array(
'step' => 'get_second_cached',
),
));
$this
->assertText('[Served output from views_content_cache]', t('Ensure the view came from the cache.'));
}
function testCachedPagedView() {
for ($i = 0; $i < 4; $i++) {
$stories[] = $this
->drupalCreateNode(array(
'type' => 'story',
));
}
sleep(1);
$this
->drupalGet('views_content_cache/latest_stories/paged');
$this
->drupalGet('views_content_cache/latest_stories/paged');
$this
->assertText('[Served output from views_content_cache]', t('Ensure the view came from the cache.'));
$this
->assertText($stories[0]->title, t('Ensure First story is rendered by the view.'));
$this
->assertText($stories[1]->title, t('Ensure Second story is rendered by the view.'));
$this
->assertNoText($stories[2]->title, t('Ensure Third story is not rendered by the view.'));
$this
->clickLink('next ›');
$this
->assertNoText('[Served output from views_content_cache]', t("Ensure the view didn't come from the cache."));
$this
->assertText($stories[2]->title, t('Ensure Third story is rendered by the view.'));
$this
->assertNoText($stories[0]->title, t('Ensure Second story is not rendered by the view.'));
$this
->drupalGet($this
->getUrl());
$this
->assertText('[Served output from views_content_cache]', t('Ensure the view came from the cache.'));
$this
->assertText($stories[3]->title, t('Ensure Fourth story is rendered by the view.'));
$this
->assertNoText($stories[1]->title, t('Ensure Second story is not rendered by the view.'));
$stories[] = $this
->drupalCreateNode(array(
'type' => 'story',
));
sleep(1);
$this
->drupalGet($this
->getUrl());
$this
->assertNoText('[Served output from views_content_cache]', t("Ensure the view didn't come from the cache."));
$this
->assertText($stories[3]->title, t('Ensure Fourth story is rendered by the view.'));
$this
->assertNoText($stories[4]->title, t('Ensure Fifth story is not rendered by the view.'));
$this
->assertLink('next ›', 0, 'Ensure that the pager has an additional page');
}
public function setUp() {
parent::setUp('views_content_cache', 'ctools', 'views', 'views_content_cache_test');
$this->privileged_user = $this
->drupalCreateUser(array(
'create story content',
'edit own story content',
'create page content',
'edit own page content',
));
$this
->drupalLogin($this->privileged_user);
}
}
class ViewsContentCacheUnitTest extends DrupalWebTestCase {
function getInfo() {
return array(
'name' => t('Views content cache unit tests'),
'description' => t('Unit tests for the Views content cache module.'),
'group' => t('Views content cache'),
);
}
public function setUp() {
parent::setUp('views_content_cache', 'ctools', 'views', 'comment');
}
function testQueryBuilder() {
$query_arrays = array(
array(
'#glue' => 'AND',
array(
'#clause' => 'FIELDA = %d',
'#args' => array(
1,
),
),
array(
'#clause' => 'FIELDB = %d',
'#args' => array(
2,
),
),
array(
'#clause' => 'FIELDC = %d',
'#args' => array(
3,
),
),
),
array(
'#glue' => 'OR',
array(
'#clause' => 'FIELDA = %d',
'#args' => array(
3,
),
),
array(
'#clause' => 'FIELDB = %d',
'#args' => array(
2,
),
),
array(
'#clause' => 'FIELDC = %d',
'#args' => array(
1,
),
),
),
array(
'#glue' => 'OR',
array(
'#glue' => 'AND',
array(
'#clause' => 'FIELDA = %d',
'#args' => array(
1,
2,
),
),
array(
'#clause' => 'FIELDB = %d',
'#args' => array(
3,
4,
),
),
),
array(
'#clause' => 'FIELDC = %d',
'#args' => array(
5,
6,
),
),
),
array(
'#glue' => 'OR',
array(
'#clause' => 'FIELDA = %d',
'#args' => array(
5,
6,
),
),
array(
'#glue' => 'AND',
array(
'#clause' => 'FIELDB = %d',
'#args' => array(
1,
2,
),
),
array(
'#clause' => 'FIELDC = %d',
'#args' => array(
3,
4,
),
),
),
),
array(
'#glue' => 'OR',
array(
'#clause' => 'FIELDA = %d',
'#args' => array(
5,
6,
),
),
array(
'#glue' => 'AND',
array(
'#clause' => 'FIELDB = %d',
'#args' => array(
1,
2,
),
),
array(
'#glue' => 'OR',
array(
'#clause' => 'FIELDC = %d',
'#args' => array(
3,
4,
),
),
array(
'#clause' => 'FIELDD = %d',
'#args' => array(
7,
8,
),
),
),
array(
'#clause' => 'FIELDE = %d',
'#args' => array(
9,
10,
),
),
),
),
);
$expected_where = array(
'(FIELDA = %d AND FIELDB = %d AND FIELDC = %d)',
'(FIELDA = %d OR FIELDB = %d OR FIELDC = %d)',
'((FIELDA = %d AND FIELDB = %d) OR FIELDC = %d)',
'(FIELDA = %d OR (FIELDB = %d AND FIELDC = %d))',
'(FIELDA = %d OR (FIELDB = %d AND (FIELDC = %d OR FIELDD = %d) AND FIELDE = %d))',
);
$expected_args = array(
array(
1,
2,
3,
),
array(
3,
2,
1,
),
array(
1,
2,
3,
4,
5,
6,
),
array(
5,
6,
1,
2,
3,
4,
),
array(
5,
6,
1,
2,
3,
4,
7,
8,
9,
10,
),
);
foreach ($query_arrays as $key => $query) {
$built = views_content_cache_query_builder($query);
$this
->assertEqual($built['#clause'], $expected_where[$key], t('Built the query: "@query", expected: "@expected".', array(
'@query' => $built['#clause'],
'@expected' => $expected_where[$key],
)));
$this
->assertIdentical($built['#args'], $expected_args[$key], t('Built the query arguments.'));
}
}
}