You are here

function imagecache_testsuite_page in ImageCache Actions 7

Same name and namespace in other branches
  1. 8 tests/imagecache_testsuite.module \imagecache_testsuite_page()

Returns the test suite page.

The test suite page contians img links to all image derivatives to create as part of the test suite.

Samples to test are scanned from:

  • The existing image styles.
  • The file features.inc attached to this module. (@todo: no longer eisting?)
  • Individual *.imagecache_preset.inc files found near any known modules.

Images illustrating the named preset are looked for also.

Flushes the entire test cache every time anything is done.

Return value

string The html for the page.

1 string reference to 'imagecache_testsuite_page'
imagecache_testsuite_menu in tests/imagecache_testsuite.module
Implementation of hook_menu().

File

tests/imagecache_testsuite.module, line 94

Code

function imagecache_testsuite_page() {
  module_load_include('inc', 'image', 'image.admin');
  module_load_include('inc', 'image', 'image.effects');
  $tests = array_merge(image_styles(), imagecache_testsuite_get_tests());
  $toolkits = image_get_available_toolkits();

  // Present the all-in-one overview page.
  $sample_folders = imagecache_testsuite_get_folders();

  // Draw the admin table.
  $test_table = array();
  foreach ($tests as $style_name => $style) {

    // Firstly, remove any previous images for the current style
    image_style_flush($style);
    $row = array();
    $row_class = 'test';
    $details_list = array();

    // Render the details.
    foreach ($style['effects'] as $effect) {
      if (!isset($effect['name'])) {

        // badness
        watchdog('imagecache_testsuite', 'invalid testcase within %style_name. No effect name', array(
          '%style_name' => $style_name,
        ), WATCHDOG_ERROR);
        $details_list[] = '<div>Unidentified effect</div>';
        $row_class = 'error';
        continue;
      }
      $effect_definition = image_effect_definition_load($effect['name']);
      if (function_exists($effect_definition['effect callback'])) {
        $description = "<strong>{$effect_definition['label']}</strong> ";
        $description .= isset($effect_definition['summary theme']) ? theme($effect_definition['summary theme'], array(
          'data' => $effect['data'],
        )) : '';
        $details_list[] = "<div>{$description}</div>";
      }
      else {

        // Probably an action that requires a module that is not installed.
        $strings = array(
          '%action' => $effect['name'],
          '%module' => $effect['module'],
        );
        $details_list[$effect['name']] = t("<div><b>%action unavailable</b>. Please enable %module module.</div>", $strings);
        $row_class = 'error';
      }
    }
    $row['details'] = "<h3>{$style['name']}</h3><p>" . implode($details_list) . "</p>";

    // Look for a sample image. May also be defined by the definition itself,
    // but normally assume a file named after the image style, in (one of the)
    // directories with test styles.
    foreach ($sample_folders as $sample_folder) {
      if (file_exists("{$sample_folder}/{$style_name}.png")) {
        $style['sample'] = "{$sample_folder}/{$style_name}.png";
      }
      elseif (file_exists("{$sample_folder}/{$style_name}.jpg")) {
        $style['sample'] = "{$sample_folder}/{$style_name}.jpg";
      }
    }
    if (isset($style['sample']) && file_exists($style['sample'])) {
      $sample_img = theme('image', array(
        'path' => $style['sample'],
      ));

      // I was having trouble with permissions on an OSX dev machine.
      if (!is_readable($style['sample'])) {
        $sample_img = "FILE UNREADABLE: {$style['sample']}";
      }
    }
    else {
      $sample_img = "[no sample]";
    }
    $row['sample'] = $sample_img;

    // Generate a result for each available toolkit.
    foreach ($toolkits as $toolkit => $toolkit_info) {
      $test_url = "admin/config/media/image-styles/testsuite/{$style_name}/{$toolkit}";
      $test_img = theme('image', array(
        'path' => $test_url,
        'alt' => "{$style_name}/{$toolkit}",
      ));
      $row[$toolkit] = l($test_img, $test_url, array(
        'html' => TRUE,
      ));
    }
    $test_table[$style_name] = array(
      'data' => $row,
      'class' => array(
        $row_class,
      ),
    );
  }
  $header = array_merge(array(
    'test',
    'sample',
  ), array_keys($toolkits));
  $output = theme('table', array(
    'header' => $header,
    'rows' => $test_table,
    'id' => 'imagecache-testsuite',
  ));

  // @todo: zebra striping can be disabled in D7.
  // Default system zebra-striping fails to show my transparency on white.
  drupal_add_html_head('<style  type="text/css" >#imagecache-testsuite tr.even{background-color:#EEEEEE !important;} #imagecache-testsuite td{vertical-align:top;}  #imagecache-testsuite tr.error{background-color:#FFCCCC !important;}</style>');
  return $output;
}