You are here

certificate.test in Certificate 7.3

File

certificate.test
View source
<?php

/**
 * @file certificate.test
 */

/**
 * Tests for Certificate.
 */
class CertificateTestCase extends DrupalWebTestCase {
  private $admin_user = NULL;
  public static function getInfo() {

    // Note that getInfo() strings are not translated with t().
    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',
    ));

    // Set up an activity.
    $this->contentType = $this
      ->drupalCreateContentType(array(
      'type' => 'certificate_activity',
    ));
    variable_set("certificate_certifiable_" . $this->contentType->type, TRUE);
  }

  /**
   * Test the certificate access check.
   */
  public function testCertificateAccess() {
    $u1 = $this
      ->drupalCreateUser();

    // Create an activity.
    $activity_node = $this
      ->drupalCreateNode(array(
      'type' => $this->contentType->type,
    ));

    // Check for forbidden certificate.
    $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.');

    // Set certificates to appear.
    $GLOBALS['certificate_ok'] = TRUE;
    $result = certificate_can_access_certificate($activity_node, $u1, TRUE);
    $this
      ->assertTrue($result === TRUE, 'User can access certificate.');
  }

  /**
   * Test that the user receives the correct 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);

    // We give them the permission because we have to preview it here.
    $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.');

    // Map the first letter of the user's name to the certificate.
    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.");
  }

  /**
   * Test removing a certificate mapping.
   */
  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();

    // Add mapping.
    $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.');

    // Remove mapping.
    $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.');
  }

  /**
   * Test that global mappings correctly populate courses and local overrides
   * are retained.
   */
  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();

    // Set globals
    $this
      ->drupalGet('admin/structure/certificates/mapping');
    $this
      ->drupalPost(NULL, array(
      'certificate[map][firstletter][a]' => 'cert_1',
      'certificate[map][firstletter][b]' => 'cert_2',
    ), t('Update'));

    // Verify defaults pull through
    $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.');

    // Change local mappings
    $this
      ->drupalPost(NULL, array(
      'certificate[map][firstletter][a]' => '-1',
      // Prevent
      'certificate[map][firstletter][b]' => '',
      // No action
      'certificate[map][firstletter][c]' => 'cert_1',
      'certificate[map][firstletter][d]' => 'cert_2',
    ), t('Save'));

    // Verify locals retained
    $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');
  }

  /**
   * Test the token replacement inside of certificates.
   */
  function testCertificateTemplates() {

    // We give them the permission because we have to preview it here.
    $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];

    // Map the first letter of the user's name to the certificate.
    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.");
  }

}

Classes

Namesort descending Description
CertificateTestCase Tests for Certificate.