View source
<?php
require_once __DIR__ . '/classified_test_abstract.test';
class ClassifiedTestTestBasicTest extends ClassifiedTestAbstractTest {
public static function getInfo() {
return array(
'name' => t('Classified'),
'description' => t('Basic tests for Classified.'),
'group' => t('Classified'),
);
}
public function setUp() {
$this->group = __FUNCTION__;
parent::setUp('field', 'taxonomy', 'statistics', 'classified', 'classified_test');
}
public function test0123396And0143680() {
$this->group = __FUNCTION__;
$format = 'Y-m-d H:i:s';
$vid = _classified_get('vid');
$category_field_name = _classified_get('field-category');
$term = (object) array(
'name' => $this
->randomName(8),
'description' => $this
->randomString(20),
'vid' => $vid,
);
$status = taxonomy_term_save($term);
$tid = $term->tid;
$this
->assertEqual($status, SAVED_NEW, t('Term @tid created in default vocabulary.', array(
'@tid' => $tid,
)), 'setup');
$this->group = t('new, default');
$node = $this
->createNode(array(
$category_field_name => array(
LANGUAGE_NONE => array(
0 => array(
'tid' => $tid,
),
),
),
));
$this
->assertTrue(isset($node->expires) && is_numeric($node->expires), t('node has an expires timestamp'), $this->group);
$lifetimes = _classified_get('lifetimes');
$lifetime = isset($lifetimes[$tid]) ? $lifetimes[$tid] : reset($lifetimes);
$expected_expires = $node->created + $lifetime * 60 * 60 * 24;
$this
->assertEqual($node->expires, $expected_expires, t('Node expire date %actual matches expected date: %expected', array(
'%actual' => format_date($node->expires, 'custom', $format),
'%expected' => format_date($expected_expires, 'custom', $format),
)), $this->group);
$this->group = t('new, reset');
$lifetimes[$tid] = $random = mt_rand(0, 90);
variable_set('classified-lifetimes', $lifetimes);
$lifetimes = _classified_get('lifetimes');
$lifetime = isset($lifetimes[$tid]) ? $lifetimes[$tid] : reset($lifetimes);
$this
->assertEqual($random, $lifetime, t('Lifetime correctly set to @random = @lifetime', array(
'@random' => $random,
'@lifetime' => $lifetime,
)), $this->group);
$node = $this
->createNode(array(
$category_field_name => array(
LANGUAGE_NONE => array(
0 => array(
'tid' => $tid,
),
),
),
'expire_mode' => 'reset',
));
$this
->assertTrue(isset($node->expires) && is_numeric($node->expires), t('node has an expires timestamp'), $this->group);
$expected_expires = $node->created + $lifetime * 60 * 60 * 24;
$this
->assertEqual($node->expires, $expected_expires, t('Node expire date %actual matches expected date: %expected', array(
'%actual' => format_date($node->expires, 'custom', $format),
'%expected' => format_date($expected_expires, 'custom', $format),
)), $this->group);
$this->group = t('new, force');
$expected_expires = REQUEST_TIME + mt_rand(0, 90 * 24 * 60 * 60);
$this
->pass(t('Limit set to @date', array(
'@date' => format_date($expected_expires, 'custom', $format),
)), $this->group);
$node = $this
->createNode(array(
$category_field_name => array(
LANGUAGE_NONE => array(
0 => array(
'tid' => $tid,
),
),
),
'expire_mode' => 'force',
'expire_date_ts' => $expected_expires,
));
$this
->assertTrue(isset($node->expires) && is_numeric($node->expires), t('node has an expires timestamp'), $this->group);
$this
->assertEqual($node->expires, $expected_expires, t('Node expire date %actual matches expected date: %expected', array(
'%actual' => format_date($node->expires, 'custom', $format),
'%expected' => format_date($expected_expires, 'custom', $format),
)), $this->group);
}
public function test0547214() {
$this->group = __FUNCTION__;
$accounts = array(
'admin',
'basic',
'creator',
);
$this
->createUsers($accounts);
foreach ($accounts as $account_name) {
$this
->drupalLogin($this->{$account_name . 'User'});
$this
->assertLink(t('Ads'), 0, t('User %name (@uid) sees "Ads" tab on his own account page', array(
'%name' => $account_name,
'@uid' => $this->{$account_name . 'User'}->uid,
)), 'Ads Tab');
$this
->drupalGet('user/' . $this->adminUser->uid);
if ($account_name == 'creator') {
$this
->assertLink(t('Ads'), 0, t('User %name sees "Ads" tab on other account pages', array(
'%name' => $account_name,
)), 'Ads Tab');
}
elseif ($account_name == 'basic') {
$this
->assertNoLink(t('Ads'), t('User %name does not see "Ads" tab on other account pages', array(
'%name' => $account_name,
)), 'Ads Tab');
}
$this
->drupalLogout();
}
}
public function test1244300() {
$this->group = __FUNCTION__;
$this
->assertFalse(in_array('create classified ad content', array_keys(module_invoke_all('permission'))), t('D6-style node creation permission is not valid'), $this->group);
$permissions = array(
'create classified content',
);
$this
->assertTrue($this
->checkPermissions($permissions, TRUE), t('D7-style node creation permission is valid'), $this->group);
user_role_revoke_permissions(DRUPAL_ANONYMOUS_RID, $permissions);
$this
->drupalGet('node/add/classified');
$this
->assertResponse(403, t('Anonymous user without %permissions may not access the ad creation form', array(
'%permissions' => implode(', ', $permissions),
)), $this->group);
user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, $permissions);
$this
->drupalGet('node/add/classified');
$this
->assertResponse(200, t('Anonymous user with %permissions may access the ad creation form', array(
'%permissions' => implode(', ', $permissions),
)), $this->group);
}
protected function relativizeHref($href) {
$base = base_path();
if (strpos($href, $base) === 0) {
$ret = drupal_substr($href, drupal_strlen($base));
}
else {
$ret = '/';
}
return $ret;
}
public function test1265524() {
$this->group = __FUNCTION__;
$this
->createUsers(array(
'admin',
));
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/structure/taxonomy/classified_categories');
$links = $this
->xpath('//a[normalize-space(text())=:label]', array(
':label' => t('edit'),
));
$this
->assert(count($links) == 2, t('Found 2 "edit" links'), $this->group);
$pattern = 'taxonomy/term/*/edit';
foreach ($links as $link) {
$href = (string) $link['href'];
$href = $this
->relativizeHref($href);
$parsed = drupal_parse_url($href);
$path = $parsed['path'];
$this
->assertTrue(drupal_match_path($path, $pattern), t('Path %path matches %pattern', array(
'%path' => $path,
'%pattern' => $pattern,
)), $this->group);
}
}
public function test1287674() {
$this->group = __FUNCTION__;
$this
->createUsers(array(
'admin',
));
$this
->pass(t('Enable "count content views".'), $this->group);
variable_set('statistics_count_content_views', 1);
$deltas = array(
'popular',
'recent',
);
$this
->drupalLogin($this->adminUser);
foreach ($deltas as $delta) {
$block = array();
$block['module'] = 'classified';
$block['delta'] = $delta;
$edit = array();
$edit['blocks[' . $block['module'] . '_' . $block['delta'] . '][region]'] = 'sidebar_first';
$this
->drupalPost('admin/structure/block', $edit, t('Save blocks'));
}
$this
->pass('Classified blocks enabled', $this->group);
$vid = _classified_get('vid');
$category_field_name = _classified_get('field-category');
$term_name = $this
->randomName(4) . " ' " . $this
->randomName(4);
$term = (object) array(
'name' => $term_name,
'description' => $this
->randomString(20),
'vid' => $vid,
);
$status = taxonomy_term_save($term);
$tid = $term->tid;
$this
->assertEqual($status, SAVED_NEW, t('Term @tid created in default vocabulary.', array(
'@tid' => $tid,
)), 'setup');
$quoted_title = $this
->randomName(4) . " ' " . $this
->randomName(4);
$node = $this
->createNode(array(
$category_field_name => array(
LANGUAGE_NONE => array(
0 => array(
'tid' => $tid,
),
),
),
'title' => $quoted_title,
));
$this
->drupalGet('node/' . $node->nid);
foreach ($deltas as $delta) {
$content = $this
->xpath("//*[@id='block-classified-{$delta}']/*[@class='content']/descendant::a[@href]");
$content = reset($content);
$this
->assertNotIdentical(FALSE, strpos($content, $quoted_title), t('Title found in @delta block', array(
'@delta' => $delta,
)), $this->group);
$this
->assertNotIdentical(FALSE, strpos($content, $term_name), t('Term found in @delta block', array(
'@delta' => $delta,
)), $this->group);
}
if (FALSE) {
$pattern = implode(array(
'/',
t('Recent ads'),
'.*',
htmlentities($quoted_title, ENT_QUOTES),
' \\(',
htmlentities($term_name, ENT_QUOTES),
'\\).*',
t('Popular ads'),
'/ms',
));
debug($pattern);
$this
->assertPattern($pattern, t('Ad correctly listed in the "Recent ads" block.'), $this->group);
}
}
public function test1382234() {
$this->group = __FUNCTION__;
$accounts = array(
'creator',
);
$this
->createUsers($accounts);
$this
->drupalLogin($this->creatorUser);
cache_clear_all('classified:overview', 'cache');
$this
->drupalGet('classified');
$this
->assertLink(t('Add one'), 0, t('Creator sees ad creation link.'));
$this
->drupalLogout();
$this
->drupalGet('classified');
$this
->assertNoLink(t('Add one'), t('Anonymous users do not see ad creation link.'));
cache_clear_all('classified:overview', 'cache');
$this
->drupalGet('classified');
$this
->assertNoLink(t('Add one'), t('Anonymous users do not see ad creation link.'));
$this
->drupalLogin($this->creatorUser);
$this
->drupalGet('classified');
$this
->assertLink(t('Add one'), 0, t('Creator sees ad creation link.'));
}
public function test1412840() {
$this->group = __FUNCTION__;
$this
->drupalGet('classified-test/invalid-term-path');
$this
->assertNoText('Trying to get property of non-object', t('Invalid taxonomy URL did not cause a syntax notice'), $this->group);
}
public function test1432606() {
$this->group = __FUNCTION__;
$accounts = array(
'admin',
'basic',
'creator',
);
$this
->createUsers($accounts);
$vid = _classified_get('vid');
$category_field_name = _classified_get('field-category');
$term = (object) array(
'name' => $this
->randomName(8),
'description' => $this
->randomString(20),
'vid' => $vid,
);
$status = taxonomy_term_save($term);
$tid = $term->tid;
$this
->assertEqual($status, SAVED_NEW, t('Term @tid created in default vocabulary.', array(
'@tid' => $tid,
)), 'setup');
$node = $this
->createNode(array(
$category_field_name => array(
LANGUAGE_NONE => array(
0 => array(
'tid' => $tid,
),
),
),
'uid' => $this->creatorUser->uid,
'status' => 1,
));
$path = 'node/' . $node->nid;
$this
->drupalLogout();
$this
->drupalGet($path);
$this
->assertResponse(200, t('Anonymous user sees published ad.'));
$this
->drupalLogin($this->adminUser);
$this
->drupalGet($path);
$this
->assertResponse(200, t('Ads administrator sees published ad.'));
$this
->drupalLogin($this->basicUser);
$this
->drupalGet($path);
$this
->assertResponse(200, t('Non-author user sees published ad.'));
$this
->drupalLogin($this->creatorUser);
$this
->drupalGet($path);
$this
->assertResponse(200, t('Author sees his published ad.'));
$node->status = 0;
node_save($node);
$this
->drupalLogout();
$this
->drupalGet($path);
$this
->assertResponse(403, t('Anonymous user does not see unpublished ad.'));
$this
->drupalLogin($this->adminUser);
$this
->drupalGet($path);
$this
->assertResponse(200, t('Ads administrator sees unpublished ad.'));
$this
->drupalLogin($this->basicUser);
$this
->drupalGet($path);
$this
->assertResponse(403, t('Non-author user does not see unpublished ad.'));
$this
->drupalLogin($this->creatorUser);
$this
->drupalGet($path);
$this
->assertResponse(200, t('Author sees his unpublished ad.'));
}
public function test1491880() {
$this->group = __FUNCTION__;
$accounts = array(
'creator',
);
$this
->createUsers($accounts);
$vid = _classified_get('vid');
$category_field_name = _classified_get('field-category');
$term = (object) array(
'name' => $this
->randomName(8),
'description' => $this
->randomString(20),
'vid' => $vid,
);
$status = taxonomy_term_save($term);
$tid = $term->tid;
$this
->assertEqual($status, SAVED_NEW, t('Term @tid created in default vocabulary.', array(
'@tid' => $tid,
)), 'setup');
$title = check_plain($this
->randomName(20));
$node = $this
->createNode(array(
$category_field_name => array(
LANGUAGE_NONE => array(
0 => array(
'tid' => $tid,
),
),
),
'uid' => $this->creatorUser->uid,
'title' => $title,
'status' => 1,
));
$tokens = token_info();
$tokens = $tokens['tokens']['user'];
$token_names = array(
'classified-ads',
'classified-ads-plain',
'classified-ads-url',
);
foreach ($token_names as $token) {
$this
->assertFalse(empty($tokens[$token]), t('%token token found in user tokens.', array(
'%token' => $token,
)), $this->group);
}
$token = 'classified-ads';
$translation = token_replace("[user:{$token}]", array(
'user' => $this->creatorUser,
));
$this
->assertTrue(preg_match('/' . $node->title . '/', $translation), t('Node title from %token.', array(
'%token' => $token,
)), $this->group);
$token = 'classified-ads-plain';
$translation = token_replace("[user:{$token}]", array(
'user' => $this->creatorUser,
));
$this
->assertTrue(preg_match('/' . $node->title . '/', $translation), t('Node title from %token.', array(
'%token' => $token,
)), $this->group);
$token = 'classified-ads-url';
$translation = token_replace("[user:{$token}]", array(
'user' => $this->creatorUser,
));
$url = url('user/' . $node->uid . '/classified', array(
'absolute' => TRUE,
));
$url = str_replace('?', '\\?', $url);
$this
->assertTrue(preg_match("@{$url}@", $translation), t('User ads URL found from %token.', array(
'%token' => $token,
)), $this->group);
}
public function test1653560() {
$this->group = __FUNCTION__;
$accounts = array(
'creator',
);
$this
->createUsers($accounts);
$this
->drupalLogin($this->creatorUser);
$type = node_type_get_name('classified');
$vid = _classified_get('vid');
$term = (object) array(
'name' => $this
->randomName(8),
'description' => $this
->randomString(20),
'vid' => $vid,
);
$status = taxonomy_term_save($term);
$tid = $term->tid;
$this
->assertEqual($status, SAVED_NEW, t('Term @tid created in default vocabulary.', array(
'@tid' => $tid,
)), 'setup');
$max_length = 20;
variable_set('classified-max-length', $max_length);
$count = db_query('SELECT COUNT(nid) FROM {node}')
->fetchField();
$this
->assertEqual(0, $count, t('No node found before creation of first ad'), $this->group);
$langcode = LANGUAGE_NONE;
$body_key = "body[{$langcode}][0][value]";
$term_key = "classified_category[{$langcode}]";
$edit = array(
'title' => $this
->randomString(10),
$term_key => $tid,
$body_key => 'A short body',
'expire_mode' => 'reset',
);
$this
->drupalPost('node/add/classified', $edit, t('Save'));
$raw = t('@type %title has been created.', array(
'@type' => $type,
'%title' => $edit['title'],
));
$this
->assertRaw($raw, t('Ad with proper body receives creation confirmation'), $this->group);
$count = db_query('SELECT COUNT(nid) FROM {node}')
->fetchField();
$this
->assertEqual(1, $count, t('Ad with proper body actually created.'), $this->group);
$edit = array(
'title' => $this
->randomString(10),
$term_key => $tid,
$body_key => 'A body longer than 20 characters',
);
$this
->drupalPost('node/add/classified', $edit, t('Save'));
$raw = t('Text is longer than maximum authorized length: @body_length characters vs @max_length authorized.', array(
'@body_length' => drupal_strlen($edit[$body_key]),
'@max_length' => $max_length,
));
$this
->assertRaw($raw, t('Ad with extra-long body receives post error.'), $this->group);
$count = db_query('SELECT COUNT(nid) FROM {node}')
->fetchField();
$this
->assertEqual(1, $count, t('Ad with extra-long body actually not created.'), $this->group);
}
public function test1733594() {
$this->group = 'Infinite';
$vid = _classified_get('vid');
$category_field_name = _classified_get('field-category');
$term = (object) array(
'name' => $this
->randomName(8),
'description' => $this
->randomString(20),
'vid' => $vid,
);
$status = taxonomy_term_save($term);
$tid = $term->tid;
$this
->assertEqual($status, SAVED_NEW, t('Term @tid created in default vocabulary.', array(
'@tid' => $tid,
)), 'setup');
$node = $this
->createNode(array(
$category_field_name => array(
LANGUAGE_NONE => array(
0 => array(
'tid' => $tid,
),
),
),
));
$lifetimes = _classified_get('lifetimes');
$default_lifetime = reset($lifetimes);
$this
->assertTrue(isset($node->expires) && is_numeric($node->expires) && $node->expires - $node->created == $default_lifetime * 24 * 60 * 60, t('node has correct timestamp'), $this->group);
variable_set('classified-grace', -1);
$future = $node->expires + 365 * 24 * 60 * 60;
$ads = _classified_scheduled_build_purge($future);
$this
->assertTrue(empty($ads), "No ad has been reported as deleted upon purge.", $this->group);
$node2 = node_load($node->nid, NULL, TRUE);
$this
->assertTrue(isset($node2->nid) && $node2->nid == $node->nid, 'Node is still present in database after purge.', $this->group);
$this->group = "Finite";
variable_set('classified-grace', 1);
$future = $node->expires + 7 * 24 * 60 * 60;
$ads = _classified_scheduled_build_purge($future);
$this
->assertTrue(count($ads) == 1, "Purges happened for one account.", $this->group);
$purges = reset($ads);
$this
->assertTrue(count($purges) == 1, "One node was purged", $this->group);
$this
->assertEqual(key($purges), $node->nid, "Nid of purged node is correct", $this->group);
$this
->assertEqual(current($purges), $node->title, "Title of purged node is correct", $this->group);
$node2 = node_load($node->nid, NULL, TRUE);
$this
->assertFalse($node2, "Node is no longer in database", $this->group);
}
public function testSecurity146738() {
$this->group = 'XSS';
$vid = _classified_get('vid');
$name = <<<XSS
term<script type="text/javascript">alert('XSS');</script>name
XSS;
$term = (object) array(
'name' => $name,
'description' => $this
->randomString(20),
'vid' => $vid,
);
$status = taxonomy_term_save($term);
$tid = $term->tid;
$this
->assertEqual($status, SAVED_NEW, t('Term @tid created in default vocabulary.', array(
'@tid' => $tid,
)), 'setup');
$this
->createUsers(array(
'admin',
));
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/config/content/classified');
$this
->assertResponse(200, t('Admin sees Classified administration.'));
$this
->assertNoRaw($name);
$this
->assertRaw(check_plain($name));
}
}