View source
<?php
class CertificateTestCase extends DrupalWebTestCase {
private $admin_user = NULL;
public static function getInfo() {
return array(
'name' => 'Certificate',
'description' => 'Ensure that the Certificate module functions properly.',
'group' => 'Certificate',
);
}
public function setUp() {
parent::setUp(array(
'certificate',
'certificate_test',
));
$this->admin_user = $this
->drupalCreateUser(array(
'bypass node access',
'administer certificates',
));
$this->contentType = $this
->drupalCreateContentType(array(
'type' => 'certificate_activity',
));
variable_set("certificate_certifiable_" . $this->contentType->type, TRUE);
}
public function testCertificateAccess() {
$u1 = $this
->drupalCreateUser();
$activity_node = $this
->drupalCreateNode(array(
'type' => $this->contentType->type,
));
$result = certificate_can_access_certificate($activity_node, $u1);
$this
->assertTrue($result !== TRUE, 'User cannot access certificate.');
$this
->assertTrue($result == 'Custom access denied message.', 'Error message matched module provided message.');
$GLOBALS['certificate_ok'] = TRUE;
$result = certificate_can_access_certificate($activity_node, $u1, TRUE);
$this
->assertTrue($result === TRUE, 'User can access certificate.');
}
public function testCertificateMapping() {
$activity_node = $this
->drupalCreateNode(array(
'type' => $this->contentType->type,
));
$content = array(
'title' => 'Test Certificate',
'name' => 'test_certificate',
'orientation' => 'portrait',
'certificate_body' => array(
LANGUAGE_NONE => array(
array(
'value' => 'My Certificate Body 1',
),
),
),
'type' => 'certificate',
);
$certificate = entity_create('certificate', $content);
entity_save('certificate', $certificate);
$u1 = $this
->drupalCreateUser(array(
'administer certificates',
));
$firstletter = $u1->name[0];
$this
->drupalLogin($u1);
$GLOBALS['certificate_ok'] = TRUE;
$this
->drupalGet("node/{$activity_node->nid}/certificate", array(
'query' => array(
'certificate_ok' => 1,
'preview' => TRUE,
),
));
$this
->assertNoResponse(403, 'Did not get access denied.');
$this
->assertNoText('Custom access denied message.', 'Did not find module provided access denied message on certificate page.');
$this
->assertText('Sorry, there is no certificate available.', 'Found no certificate available text.');
certificate_update_node_mappings($activity_node->nid, array(
'firstletter' => array(
$firstletter => $certificate->name,
),
));
$this
->drupalGet("node/{$activity_node->nid}/certificate", array(
'query' => array(
'certificate_ok' => 1,
'preview' => TRUE,
),
));
$this
->assertNoResponse(403, 'Did not get access denied.');
$this
->assertNoText('Custom access denied message.', 'Did not find module provided access denied message on certificate page.');
$this
->assertNoText('Sorry, there is no certificate available.', 'User received certificate.');
$this
->assertText("My Certificate Body 1", "Saw certificate body.");
}
public function testCertificateUnset() {
$this
->drupalLogin($this->admin_user);
$activity_node = $this
->drupalCreateNode(array(
'type' => $this->contentType->type,
));
$values1 = array(
'title' => 'Cert 1',
'name' => 'cert_1',
'orientation' => 'portrait',
'certificate_body' => array(
LANGUAGE_NONE => array(
array(
'value' => 'My Certificate Body 1',
),
),
),
'type' => 'certificate',
);
$certificate1 = entity_create('certificate', $values1);
$certificate1
->save();
$this
->drupalGet("node/{$activity_node->nid}/edit");
$this
->drupalPost(NULL, array(
'certificate[map][manual][manual]' => 'cert_1',
), t('Save'));
$this
->drupalGet("node/{$activity_node->nid}/edit");
$this
->assertOptionSelected('edit-certificate-map-manual-manual', 'cert_1', 'Certificate mapping set.');
$this
->drupalPost(NULL, array(
'certificate[map][manual][manual]' => '',
), t('Save'));
$this
->drupalGet("node/{$activity_node->nid}/edit");
$this
->assertOptionSelected('edit-certificate-map-manual-manual', '', 'Certificate mapping removed.');
}
public function testCertificateGlobalMapping() {
$this
->drupalLogin($this->admin_user);
$activity_node = $this
->drupalCreateNode(array(
'type' => $this->contentType->type,
));
$values1 = array(
'title' => 'Cert 1',
'name' => 'cert_1',
'orientation' => 'portrait',
'certificate_body' => array(
LANGUAGE_NONE => array(
array(
'value' => 'My Certificate Body 1',
),
),
),
'type' => 'certificate',
);
$certificate1 = entity_create('certificate', $values1);
$certificate1
->save();
$values2 = array(
'title' => 'Cert 2',
'name' => 'cert_2',
'orientation' => 'portrait',
'certificate_body' => array(
LANGUAGE_NONE => array(
array(
'value' => 'My Certificate Body 2',
),
),
),
'type' => 'certificate',
);
$certificate2 = entity_create('certificate', $values2);
$certificate2
->save();
$this
->drupalGet('admin/structure/certificates/mapping');
$this
->drupalPost(NULL, array(
'certificate[map][firstletter][a]' => 'cert_1',
'certificate[map][firstletter][b]' => 'cert_2',
), t('Update'));
$this
->drupalGet("node/{$activity_node->nid}/edit");
$this
->assertOptionSelected('edit-certificate-map-firstletter-a', 'cert_1', 'Certificate 1 populated from global.');
$this
->assertOptionSelected('edit-certificate-map-firstletter-b', 'cert_2', 'Certificate 2 populated from global.');
$this
->drupalPost(NULL, array(
'certificate[map][firstletter][a]' => '-1',
'certificate[map][firstletter][b]' => '',
'certificate[map][firstletter][c]' => 'cert_1',
'certificate[map][firstletter][d]' => 'cert_2',
), t('Save'));
$this
->drupalGet("node/{$activity_node->nid}/edit");
$this
->assertOptionSelected('edit-certificate-map-firstletter-a', '-1', '"a" mapping populuated from local');
$this
->assertOptionSelected('edit-certificate-map-firstletter-b', '', '"b" mapping populuated from local');
$this
->assertOptionSelected('edit-certificate-map-firstletter-c', 'cert_1', '"c" mapping populuated from local');
$this
->assertOptionSelected('edit-certificate-map-firstletter-d', 'cert_2', '"d" mapping populuated from local');
}
function testCertificateTemplates() {
$account = $this
->drupalCreateUser(array(
'administer certificates',
));
$activity_node = $this
->drupalCreateNode(array(
'title' => 'My test certifiable type',
'type' => $this->contentType->type,
));
$content = array(
'title' => 'Test Certificate',
'name' => 'test_certificate',
'orientation' => 'portrait',
'certificate_body' => array(
LANGUAGE_NONE => array(
array(
'value' => 'My Certificate Body [node:title] [user:name]',
),
),
),
'type' => 'certificate',
);
$certificate = entity_create('certificate', $content);
entity_save('certificate', $certificate);
$firstletter = $account->name[0];
certificate_update_node_mappings($activity_node->nid, array(
'firstletter' => array(
$firstletter => $certificate->name,
),
));
$this
->drupalLogin($account);
$this
->drupalGet("node/{$activity_node->nid}/certificate", array(
'query' => array(
'certificate_ok' => 1,
'preview' => TRUE,
),
));
$this
->assertText("My Certificate Body {$activity_node->title} {$account->name}", "Saw certificate body.");
}
}