You are here

cronkeychange.test in Cron key change 7

Tests the Cron key change module.

File

cronkeychange.test
View source
<?php

/**
 * @file
 * Tests the Cron key change module.
 */

/**
 * Tests the Cron key change module.
 */
class CronKeyChange extends DrupalWebTestCase {

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'Change cron key',
      'description' => 'Test the change cron key.',
      'group' => 'Cron key change',
    );
  }

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp('cronkeychange');

    // Create and log in our privileged user.
    $account = $this
      ->drupalCreateUser(array(
      'administer site configuration',
    ));
    $this
      ->drupalLogin($account);
  }

  /**
   * Tests the change cron key.
   */
  public function testCronKeyChange() {
    $original_cron_key = variable_get('cron_key');
    $this
      ->drupalGet('admin/config/system/cron');
    $visible_cron_key = trim($this
      ->getXpathTextValue($this
      ->drupalGetContent(), "id('edit-current')/text()[2]"));
    $this
      ->assertEqual($original_cron_key, $visible_cron_key, 'Original value show correctly.');

    // Generate new key.
    $this
      ->drupalPost('admin/config/system/cron', array(), t('Generate new key'));
    $visible_cron_key = trim($this
      ->getXpathTextValue($this
      ->drupalGetContent(), "id('edit-current')/text()[2]"));
    $this
      ->assertNotIdentical($visible_cron_key, '', 'Generated cron key is not null.');
    $this
      ->assertNotEqual($original_cron_key, $visible_cron_key, 'Cron key is changed.');
  }

  /**
   * Get text value from document.
   */
  private function getXpathTextValue($document, $path) {
    $result = array();
    $doc = new DOMDocument();
    $doc
      ->loadHTML($document);
    $xpath = new DOMXPath($doc);
    $elements = $xpath
      ->query($path);
    if (!is_null($elements)) {
      foreach ($elements as $element) {
        $result = $element->wholeText;
      }
    }
    return $result;
  }

}

Classes

Namesort descending Description
CronKeyChange Tests the Cron key change module.