View source
<?php
class BiblioWebTestCase extends DrupalWebTestCase {
protected $kids = array();
protected $cids = array();
protected $nids = array();
protected $admin_user;
function createNode($type = 100) {
$schema = drupal_get_schema('biblio');
foreach ($schema['fields'] as $name => $values) {
if ($values['type'] == 'int') {
continue;
}
switch ($values['type']) {
case 'varchar':
$length = $values['length'];
break;
case 'text':
$length = 1000;
break;
}
$biblio_fields["{$name}"] = $name;
}
$settings = array(
'title' => 'Biblio Title',
'type' => 'biblio',
'biblio_type' => $type,
'biblio_year' => 2009,
'biblio_contributors' => array(
0 => array(
'name' => 'Ron J. Jeromezzzzzz',
'auth_type' => 1,
'auth_category' => 1,
),
1 => array(
'name' => 'John Smithzzzzzz',
'auth_type' => 1,
'auth_category' => 1,
),
2 => array(
'name' => 'George W. Bushzzzzzz',
'auth_type' => 1,
'auth_category' => 1,
),
),
'biblio_keywords' => array(
'biblio_keywords',
),
);
$settings = array_merge($biblio_fields, $settings);
$node = $this
->drupalCreateNode($settings);
$node = node_load($node->nid, NULL, TRUE);
foreach ($node->biblio_contributors as $author) {
$this->cids[] = $author['cid'];
}
$this->nids[] = $node->nid;
return $node;
}
function assertBiblioFields($node1, $node2, $fields = array()) {
$count = 0;
foreach ($fields as $field) {
if ($node1->{$field} != $node2->{$field}) {
$this
->assertIdentical($node1->{$field}, $node2->{$field});
$count++;
}
}
$this
->assertEqual($count, 0, "There were {$count} differences between the two nodes");
}
}
class BiblioNodeCreationTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Biblio node creation',
'description' => 'Create a biblio node and test saving it.',
'group' => 'Biblio',
);
}
function setUp() {
parent::setUp('biblio');
$web_user = $this
->drupalCreateUser(array(
'create biblio content',
));
$this
->drupalLogin($web_user);
}
function testBiblioNodeCreation() {
$edit = array();
$edit["biblio_type"] = '101';
$this
->drupalPost('node/add/biblio', $edit, t('Next'));
$this
->assertOptionSelected('edit-biblio-type', '101');
$this
->assertFieldById('edit-title');
$this
->assertFieldById('edit-biblio-year');
$edit = array(
'title' => $this
->randomString(32),
'biblio_year' => '2009',
'biblio_contributors[0][name]' => 'Kevin Brown',
'biblio_contributors[1][name]' => 'Martin Clark',
'biblio_contributors[2][name]' => 'George Wei',
'biblio_keywords' => 'architecture, building, wood',
);
$this
->drupalPost(NULL, $edit, t('Save'));
$this
->assertRaw(t('!post %title has been created.', array(
'!post' => 'Biblio',
'%title' => $edit["title"],
)), t('Biblio entry created.'));
$this
->assertText(t('architecture, building, wood', array(
'!post' => 'Biblio',
'%title' => $edit["title"],
)), t('Keywords are present on the biblio node.'));
$node = $this
->drupalGetNodeByTitle($edit['title']);
$this
->assertTrue($node, t('Node found in database.'));
}
}
class BiblioPageViewTestCase extends BiblioWebTestCase {
public static function getInfo() {
return array(
'name' => 'Biblio node view and edit permissions',
'description' => 'Create a biblio node and test view / edit permissions.',
'group' => 'Biblio',
);
}
function setUp() {
parent::setUp('biblio');
}
function testBiblioPageView() {
$node = $this
->createNode('101');
$this
->assertTrue(node_load($node->nid), t('Node created.'));
$html = $this
->drupalGet("node/{$node->nid}/edit");
$this
->assertResponse(403);
$web_user = $this
->drupalCreateUser(array(
'access content',
));
$this
->drupalLogin($web_user);
$this
->drupalGet("node/{$node->nid}/edit");
$this
->assertResponse(403);
$web_user = $this
->drupalCreateUser(array(
'edit any biblio content',
));
$this
->drupalLogin($web_user);
$this
->drupalGet("node/{$node->nid}/edit");
$this
->assertResponse(200);
}
}