You are here

cleaner_base.test in Cleaner 7

File with a base class for the Cleaner test cases.

Any tests for a Cleaner module should extend this class.

File

tests/cleaner_base.test
View source
<?php

/**
 * @file
 * File with a base class for the Cleaner test cases.
 *
 * Any tests for a Cleaner module should extend this class.
 */

/**
 * Class CleanerTestBase.
 *
 * Base class for the Cleaner test cases.
 */
class CleanerTestBase extends DrupalWebTestCase {

  /**
   * Create a user with specific role.
   *
   * @param string $role
   *   Role name.
   *
   * @return false|object
   *   User account.
   */
  public function cleanerCreateUserWithRole($role) {
    $this
      ->assertTrue(!empty($role));

    // Get specific role key.
    $role_key = array_search($role, user_roles());

    // Get all permissions by role key => name.
    $perms = user_role_permissions(array(
      $role_key => $role,
    ));
    $this
      ->assertTrue(is_array($perms));
    $this
      ->assertTrue(isset($perms[$role_key]));
    $perms = $perms[$role_key];

    // Create a user with a permissions specified by role.
    return $this
      ->drupalCreateUser(array_keys($perms));
  }

  /**
   * Cleaner settings handler.
   *
   * @param array $conf
   *   An associative array with a configure data.
   *   Example of $conf array:
   *    array(
   *      'cleaner_cron'            => 1,
   *      'cleaner_clear_cache'     => 1,
   *      'cleaner_empty_watchdog'  => 0,
   *      'cleaner_clean_sessions'  => 1,
   *      'cleaner_clean_cssdir'    => 1,
   *      'cleaner_clean_jsdir'     => 1,
   *      'cleaner_optimize_db'     => 1,
   *    ).
   */
  public function cleanerSettings(array $conf) {

    // Set a configurations for Cleaner.
    $this
      ->drupalPost('admin/config/system/cleaner', $conf, t('Save configuration'));

    // Assert response.
    $this
      ->assertResponse(200);

    // Assert "saving" text.
    $this
      ->assertText('The configuration options have been saved.', 'Settings successfully saved.');
  }

  /**
   * Execute hook_cleaner_run().
   */
  public function cleanerExecute() {

    // Check if hook_cleaner_run() exist.
    $this
      ->assertTrue(module_hook('cleaner', 'cleaner_run'), 'Hook - hook_cleaner_run() exist!');

    // Execute hook_cleaner_run().
    module_invoke_all('cleaner_run');
  }

  /**
   * Save dummy cache.
   *
   * @param string $cid
   *   Cache ID(CID).
   *
   * @return string
   *   Cache value which has been set.
   */
  public function cleanerSaveDummyCache($cid) {
    $this
      ->assertTrue(is_string($cid));

    // Create the variable with a random string and save it to the cache.
    $dummy_cache = $this
      ->randomString();

    // Check if dummy cache variable has been created and it has a string type.
    $this
      ->assertTrue(is_string($dummy_cache), 'Dummy cache variable has been created');

    // Save cache.
    cache_set($cid, $dummy_cache, 'cache', REQUEST_TIME + 3600);

    // Check if dummy cache variable has been saved to the cache.
    $this
      ->assertNotEqual(cache_get($cid, 'cache'), FALSE, 'Dummy cache variable has been saved to the cache.');
    return $dummy_cache;
  }

  /**
   * Get dummy cache.
   *
   * @param string $cid
   *   Cache ID(CID).
   *
   * @return string
   *   Cache value after the cache clearing.
   */
  public function cleanerGetDummyCache($cid) {
    $this
      ->assertTrue(is_string($cid));

    // Get previously saved variable from the cache.
    return cache_get($cid);
  }

  /**
   * Go to the log page.
   */
  public function cleanerGetLog() {

    // Go to the watchdog logs.
    $this
      ->drupalGet('admin/reports/dblog');

    // Assert response.
    $this
      ->assertResponse(200);
  }

  /**
   * Basic SetUp method for administrator.
   */
  public function cleanerAdminSetUp() {

    // Enable required modules.
    parent::setUp('cleaner', 'dblog', 'syslog', 'system');

    // Check if all modules has been enabled.
    foreach (array(
      'cleaner',
      'dblog',
      'syslog',
      'system',
    ) as $module) {
      $this
        ->assertTrue(module_exists($module));
    }

    // Create site admin user.
    $admin = $this
      ->cleanerCreateUserWithRole('administrator');

    // Check if admin account has been created.
    if (!$admin || $admin == NULL) {
      $this
        ->fail("User hasn't been created.");
    }
    else {
      $this
        ->pass("User has been created successfully.");
    }

    // Login as site admin user.
    $this
      ->drupalLogin($admin);
  }

}

Classes

Namesort descending Description
CleanerTestBase Class CleanerTestBase.