View source
<?php
class PathModuleTestCase extends DrupalTestCase {
function get_info() {
return array(
'name' => t('Path alias functionality'),
'desc' => t('Add, edit, delete, and change alias and verify its consistency in the database.'),
'group' => t('Path Tests'),
);
}
function setUp() {
parent::setUp();
$this
->drupalModuleEnable('path');
$web_user = $this
->drupalCreateUserRolePerm(array(
'edit own page content',
'create page content',
'administer url aliases',
'create url aliases',
));
$this
->drupalLoginUser($web_user);
}
function testAdminAlias() {
$node1 = $this
->createNode();
$edit = array();
$edit['src'] = 'node/' . $node1->nid;
$edit['dst'] = $this
->randomName(8);
$this
->drupalPost('admin/build/path/add', $edit, 'Create new alias');
$this
->drupalGet($edit['dst']);
$this
->assertText($node1->title, 'Alias works.');
$pid = $this
->getPID($edit['dst']);
$previous = $edit['dst'];
$edit['dst'] = $this
->randomName(8);
$this
->drupalPost('admin/build/path/edit/' . $pid, $edit, 'Update alias');
$this
->drupalGet($edit['dst']);
$this
->assertText($node1->title, 'Changed alias works.');
$this
->drupalGet($previous);
$this
->assertNoText($node1->title, 'Previous alias no longer works.');
$this
->assertTitle(new PatternExpectation('/Page not found/'), 'We get page not found error');
$node2 = $this
->createNode();
$edit['src'] = 'node/' . $node2->nid;
$this
->drupalPost('admin/build/path/add', $edit, 'Create new alias');
$this
->assertWantedRaw(t('The alias %alias is already in use in this language.', array(
'%alias' => $edit['dst'],
)), 'Attempt to move alias was rejected.');
$this
->drupalPost('admin/build/path/delete/' . $pid, array(), 'Confirm');
$this
->drupalGet($edit['dst']);
$this
->assertNoText($node1->title, 'Alias was successfully deleted.');
}
function testNodeAlias() {
$node1 = $this
->createNode();
$edit = array();
$edit['path'] = $this
->randomName(8);
$this
->drupalPost('node/' . $node1->nid . '/edit', $edit, 'Save');
$this
->drupalGet($edit['path']);
$this
->assertText($node1->title, 'Alias works.');
$previous = $edit['path'];
$edit['path'] = $this
->randomName(8);
$this
->drupalPost('node/' . $node1->nid . '/edit', $edit, 'Save');
$this
->drupalGet($edit['path']);
$this
->assertText($node1->title, 'Changed alias works.');
$this
->drupalGet($previous);
$this
->assertNoText($node1->title, 'Previous alias no longer works.');
$this
->assertTitle(new PatternExpectation('/Page not found/'), 'We get page not found error');
$node2 = $this
->createNode();
$this
->drupalPost('node/' . $node2->nid . '/edit', $edit, 'Save');
$this
->assertText(t('The path is already in use.'), 'Attempt to moved alias was rejected.');
$this
->drupalPost('node/' . $node1->nid . '/edit', array(
'path' => '',
), 'Save');
$this
->drupalGet($edit['path']);
$this
->assertNoText($node1->title, 'Alias was successfully deleted.');
}
function getPID($dst) {
return db_result(db_query("SELECT pid FROM {url_alias} WHERE dst = '%s'", $dst));
}
function createNode() {
$this
->drupalVariableSet('node_options_page', array(
'status',
'promote',
));
$edit = array();
$edit['title'] = '!SimpleTest test node! ' . $this
->randomName(10);
$edit['body'] = '!SimpleTest test body! ' . $this
->randomName(32) . ' ' . $this
->randomName(32);
$this
->drupalPost('node/add/page', $edit, 'Save');
$node = node_load(array(
'title' => $edit['title'],
));
$this
->assertNotNull($node === FALSE ? NULL : $node, 'Node found in database. %s');
return $node;
}
}