| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php // $Id: imagecache_create_url.test,v 1.4 2008/07/09 03:07:11 dopry Exp $ 2 3 /** 4 * Test class for testing imagecache_create_url() in several use cases with 5 * the different combinations of clean URLs and private/public download method. 6 */ 7 class ImageCacheUrlTests extends DrupalTestCase { 8 9 /** 10 * General admin user. 11 */ 12 var $admin_user; 13 14 /** 15 * The id of the php input format. 16 */ 17 var $input_format_id; 18 19 /** 20 * Drupal SimpleTest method: return metadata about the test. 21 */ 22 function get_info() { 23 return array( 24 'name' => 'ImageCache Create URL Tests', 25 'desc' => 'Assure that URLs are properly generated by imagecache_create_url(), as discussed at http://drupal.org/node/241541', 26 'group' => 'ImageCache', 27 ); 28 } 29 30 /** 31 * SimpleTest core method: code run before each and every test method. 32 */ 33 function setUp() { 34 // Always call the setUp() function from the parent class. 35 parent::setUp(); 36 37 // Make sure that the ImageCache module is enabled. 38 $this->drupalModuleEnable('imagecache'); 39 40 // Create admin user 41 $permissions = array( 42 'administer filters', 43 ); 44 $this->admin_user = $this->drupalCreateUserRolePerm($permissions); 45 46 // Log in with admin user. 47 $this->drupalLoginUser($this->admin_user); 48 49 // Add an input format with PHP evaluator. 50 $edit = array( 51 'name' => $this->randomName(10, 'inputformat_'), 52 'filters[filter/1]' => TRUE, 53 'roles[2]' => TRUE, 54 ); 55 $this->drupalPostRequest('admin/settings/filters/add', $edit, t('Save configuration')); 56 // Store the format id of the created input format. 57 $this->input_format_id = db_result(db_query("SELECT format FROM {filter_formats} WHERE name = '%s'", $edit['name'])); 58 $this->assertTrue($this->input_format_id, t('Input format id (%s)')); 59 60 } 61 62 /** 63 * SimpleTest core method: code run after each and every test method. 64 */ 65 function tearDown() { 66 // Remove input format. 67 $this->drupalPostRequest('admin/settings/filters/delete/'. $this->input_format_id, array(), t('Delete')); 68 // Log out admin user. 69 $this->drupalGet('logout'); 70 71 // Always call the tearDown() function from the parent class. 72 parent::tearDown(); 73 } 74 75 /** 76 * Test function that tests imagecache_create_url() under 77 * the different combinations of clean URLs and file download method 78 */ 79 function testImageCacheCreateUrl() { 80 // No Clean URLs + public downloads : http://example.com/?q=path/to/files/imagecache/preset/foo.jpg 81 $this->_ImagecacheCreateUrlTest( 82 false, FILE_DOWNLOADS_PUBLIC, 83 'path/to/files', 'preset', 'foo.jpg', 84 'http://example.com/?q=path/to/files/imagecache/preset/foo.jpg' 85 ); 86 87 // Clean URLs + public downloads : http://example.com/path/to/files/imagecache/preset/foo.jpg 88 $this->_ImagecacheCreateUrlTest( 89 true, FILE_DOWNLOADS_PUBLIC, 90 'path/to/files', 'preset', 'foo.jpg', 91 'http://example.com/path/to/files/imagecache/preset/foo.jpg' 92 ); 93 94 // No Clean URLs + private downloads : http://example.com/?q=system/files/imagecache/preset/foo.jpg 95 $this->_ImagecacheCreateUrlTest( 96 false, FILE_DOWNLOADS_PRIVATE, 97 'path/to/files', 'preset', 'foo.jpg', 98 'http://example.com/?q=system/files/imagecache/preset/foo.jpg' 99 ); 100 101 // Clean URLs + private downloads : http://example.com/system/files/imagecache/preset/foo.jpg 102 $this->_ImagecacheCreateUrlTest( 103 true, FILE_DOWNLOADS_PRIVATE, 104 'path/to/files', 'preset', 'foo.jpg', 105 'http://example.com/system/files/imagecache/preset/foo.jpg' 106 ); 107 } 108 109 110 /** 111 * Function to actually perform URL tests. 112 * @param $clean_url 113 * 'clean_url' setting for test. 114 * @param $file_downloads 115 * 'file_downloads' setting for test. 116 * @param $file_directory_path 117 * 'file_directory_path' setting for tests. 118 * @param $preset 119 * imagecache preset name to be used for test. 120 * @param $path 121 * file path to be used for generating output. 122 * @param $expected 123 * the url expected as output from imagecache_create_url 124 * 125 * Note about the implementation: 126 * At first sight one might think this can be easily implemented with just 127 * setting the Drupal settings, calling imagecache_create_url() and checking 128 * the result. This does not work however because the url() function, which is 129 * used by imagecache_create_url(), caches the clean_url setting with an 130 * internal static variable. This means that only one setting of clean_url 131 * can be evaluated per page view. 132 * To make testing possible, this function creates a node with the PHP 133 * evaluator as input filter and puts a proper call to imagecache_create_url() 134 * in the node body. The node view, which is a page view on its own can then 135 * be checked for the correctly generated URL. 136 */ 137 private function _ImagecacheCreateUrlTest($clean_url, $file_downloads, $file_directory_path, $preset, $path, $expected) { 138 // Drupal settings 139 $this->drupalVariableSet('clean_url', $clean_url); 140 $this->drupalVariableSet('file_downloads', $file_downloads); 141 $this->drupalVariableSet('file_directory_path', $file_directory_path); 142 143 // Build node body (php code). 144 $body = "<?php 145 // Change base_url 146 \$GLOBALS['base_url'] = 'http://example.com'; 147 // Generate URL and check it. 148 echo imagecache_create_url('$preset', '$path'); 149 ?>"; 150 // Create node. 151 $node = $this->drupalCreateNode(array( 152 'body' => $body, 153 'format' => $this->input_format_id, 154 )); 155 156 // Show node. 157 $this->drupalGet(url('node/' . $node->nid, NULL, NULL, TRUE)); 158 159 // Check if expected url shows up 160 $this->assertWantedRaw($expected, 161 t('[ImageCacheUrlTests] @clean_url + @file_downloads should return "@expected"', array( 162 '@clean_url' => ($clean_url ? 'Clean URLs' : 'No clean URLs'), 163 '@file_downloads' => ($file_downloads == FILE_DOWNLOADS_PRIVATE ? 'private downloads' : 'public downloads'), 164 '@expected' => $expected) 165 ) 166 ); 167 } 168 169 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Mar 24 11:18:33 2011 | Cross-referenced by PHPXref 0.7 |