View source
<?php
class UserpointsNodeVisitsTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => t('Userpoints Node Visits'),
'description' => t('Tests granting points when viewing content.'),
'group' => t('Userpoints'),
);
}
function setUp() {
parent::setUp('userpoints', 'userpoints_nc_visits');
}
function testVisitingContent() {
$this->admin = $this
->drupalCreateUser(array(
'view userpoints',
'view own userpoints',
'bypass node access',
'administer nodes',
'administer userpoints',
'administer content types',
));
$this->user1 = $this
->drupalCreateUser(array(
'userpoints nc track visits',
));
$this->user2 = $this
->drupalCreateUser(array(
'userpoints nc track visits',
));
$this
->drupalLogin($this->admin);
$this->type = array(
'name' => $this
->randomName(10),
'type' => strtolower($this
->randomName(5)),
'userpoints_nc_visits_points' => 2,
);
$this
->drupalPost('admin/structure/types/add', $this->type, t('Save content type'));
$this
->assertText(t('The content type @name has been added.', array(
'@name' => $this->type['name'],
)));
node_types_rebuild();
menu_rebuild();
$this
->checkPermissions(array(), TRUE);
user_role_grant_permissions(4, array(
'create ' . $this->type['type'] . ' content',
'edit any ' . $this->type['type'] . ' content',
));
$this
->verifyAllContent();
$this
->deletePoints();
$this
->drupalLogin($this->admin);
$this
->verifyFirstContentOnly();
}
function deletePoints() {
db_truncate('userpoints')
->execute();
db_truncate('userpoints_txn')
->execute();
db_truncate('userpoints_nc_visits')
->execute();
}
function verifyAllContent() {
$edit = array(
'userpoints_nc_visits_limit' => 'all',
);
$this
->drupalPost('admin/config/people/userpoints/settings', $edit, t('Save configuration'));
$content = array(
'title' => $this
->randomName(10),
);
$this
->drupalPost('node/add/' . $this->type['type'], $content, t('Save'));
$this
->assertText(t('@name @title has been created.', array(
'@name' => $this->type['name'],
'@title' => $content['title'],
)));
preg_match('|node/(\\d+)|', $this
->getUrl(), $matches);
$nid1 = $matches[1];
$content = array(
'title' => $this
->randomName(10),
);
$this
->drupalPost('node/add/' . $this->type['type'], $content, t('Save'));
$this
->assertText(t('@name @title has been created.', array(
'@name' => $this->type['name'],
'@title' => $content['title'],
)));
preg_match('|node/(\\d+)|', $this
->getUrl(), $matches);
$nid2 = $matches[1];
$this
->assertEqual(userpoints_get_current_points($this->admin->uid), 0);
$this
->drupalLogin($this->user1);
$this
->drupalGet('node/' . $nid1);
$this
->assertEqual(userpoints_get_current_points($this->admin->uid), 2);
$this
->drupalGet('node/' . $nid1);
$this
->assertEqual(userpoints_get_current_points($this->admin->uid), 2);
$this
->drupalGet('node/' . $nid2);
$this
->assertEqual(userpoints_get_current_points($this->admin->uid), 4);
$this
->drupalLogin($this->user2);
$this
->drupalGet('node/' . $nid2);
$this
->assertEqual(userpoints_get_current_points($this->admin->uid), 6);
$this
->drupalGet('node/' . $nid1);
$this
->assertEqual(userpoints_get_current_points($this->admin->uid), 8);
$this
->drupalGet('node/' . $nid2);
$this
->assertEqual(userpoints_get_current_points($this->admin->uid), 8);
}
function verifyFirstContentOnly() {
$edit = array(
'userpoints_nc_visits_limit' => 'one',
);
$this
->drupalPost('admin/config/people/userpoints/settings', $edit, t('Save configuration'));
$content = array(
'title' => $this
->randomName(10),
);
$this
->drupalPost('node/add/' . $this->type['type'], $content, t('Save'));
$this
->assertText(t('@name @title has been created.', array(
'@name' => $this->type['name'],
'@title' => $content['title'],
)));
preg_match('|node/(\\d+)|', $this
->getUrl(), $matches);
$nid1 = $matches[1];
$content = array(
'title' => $this
->randomName(10),
);
$this
->drupalPost('node/add/' . $this->type['type'], $content, t('Save'));
$this
->assertText(t('@name @title has been created.', array(
'@name' => $this->type['name'],
'@title' => $content['title'],
)));
preg_match('|node/(\\d+)|', $this
->getUrl(), $matches);
$nid2 = $matches[1];
$this
->assertEqual(userpoints_get_current_points($this->admin->uid), 0);
$this
->drupalLogin($this->user1);
$this
->drupalGet('node/' . $nid1);
$this
->assertEqual(userpoints_get_current_points($this->admin->uid), 2);
$this
->drupalGet('node/' . $nid1);
$this
->assertEqual(userpoints_get_current_points($this->admin->uid), 2);
$this
->drupalGet('node/' . $nid2);
$this
->assertEqual(userpoints_get_current_points($this->admin->uid), 2);
$this
->drupalLogin($this->user2);
$this
->drupalGet('node/' . $nid2);
$this
->assertEqual(userpoints_get_current_points($this->admin->uid), 4);
$this
->drupalGet('node/' . $nid1);
$this
->assertEqual(userpoints_get_current_points($this->admin->uid), 4);
}
}