You are here

cleaner_files.test in Cleaner 7

Test Clearing CSS/JS files.

File

tests/cleaner_files.test
View source
<?php

/**
 * @file
 * Test Clearing CSS/JS files.
 */

/**
 * Class CleanerTestFilesClearing.
 *
 * Check is CSS/JS files are successfully cleared.
 */
class CleanerTestFilesClearing extends CleanerTestBase {

  /**
   * Setting a test info.
   *
   * @return array
   *   An array with an info data.
   */
  public static function getInfo() {
    return array(
      'name' => 'Cleaner test clearing CSS/JS files.',
      'description' => 'Check is CSS/JS files are successfully cleared.',
      'group' => 'Cleaner',
    );
  }

  /**
   * Required setup processes before run a tests.
   */
  public function setUp() {
    $this
      ->cleanerAdminSetUp();
  }

  /**
   * Test clearing CSS/JS files.
   */
  public function testFilesClearing() {

    // Prepare a Cleaner settings.
    $settings = array(
      'cleaner_clean_cssdir' => 1,
      'cleaner_clean_jsdir' => 1,
    );
    $this
      ->cleanerSettings($settings);

    // Enable CSS/JS files aggregation.
    $settings = array(
      'preprocess_css' => 1,
      'preprocess_js' => 1,
      'page_compression' => 1,
    );
    $this
      ->drupalPost('admin/config/development/performance', $settings, t('Save configuration'));

    // Refresh default homepage for generate a CSS/JS files by aggregation.
    for ($i = 0; $i < 50; $i++) {
      $this
        ->drupalGet('node');

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

    // Change file modification time.
    self::changeFilesModificationTime();

    // Save an initial file count.
    $initial_count = self::countFiles();

    // Execute Cleaner run.
    $this
      ->cleanerExecute();

    // Get files count after clearing.
    $count = self::countFiles();
    if ($count < $initial_count) {
      $this
        ->pass("CSS/JS files has been successfully cleared.");
    }
    else {
      $this
        ->fail("CSS/JS files has not been cleared.");
    }

    // Get logs.
    $this
      ->cleanerGetLog();

    // Check if CSS/JS files clearing has been successfully written to the log.
    $this
      ->assertText('Cleared old temporary CSS files by Cleaner', "CSS files clearing logged to watchdog");
    $this
      ->assertText('Cleared old temporary JS files by Cleaner', "JS files clearing logged to watchdog");
  }

  /**
   * Count CSS and JS files.
   *
   * @return int
   *   Count value.
   */
  private static function countFiles() {
    $css = count(scandir(drupal_realpath('public://css')));
    $js = count(scandir(drupal_realpath('public://js')));
    return $css + $js;
  }

  /**
   * Change file modification time.
   */
  private static function changeFilesModificationTime() {

    // Set time to -10 hours from now.
    $time = time() - 36000;

    // Make changes for CSS and JS files.
    foreach (array(
      'css',
      'js',
    ) as $type) {

      // Get files directory real path.
      $dir = drupal_realpath('public://' . $type);
      foreach (scandir($dir) as $file) {

        // Check is file name contain 'css' or 'js'.
        if (strpos($file, $type) !== FALSE) {

          // Change file modification time.
          touch($dir . '/' . $file, $time);
        }
      }

      // Clear file status cache.
      clearstatcache();
    }
  }

}

Classes

Namesort descending Description
CleanerTestFilesClearing Class CleanerTestFilesClearing.