1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
<?php
// $Id$
/**
* @file
* Image module tests.
*/
/**
* TODO: Test the following functions.
*
* image.effects.inc:
* image_style_generate()
* image_style_create_derivative()
*
* image.module:
* image_style_load()
* image_style_save()
* image_style_delete()
* image_style_options()
* image_style_flush()
* image_effect_definition_load()
* image_effect_load()
* image_effect_save()
* image_effect_delete()
* image_filter_keyword()
*/
/**
* Tests the functions for generating paths and URLs for image styles.
*/
class ImageStylesPathAndUrlUnitTest extends DrupalWebTestCase {
protected $style_name;
protected $image_with_generated;
protected $image_without_generated;
function getInfo() {
return array(
'name' => t('Image styles path and URL functions'),
'description' => t('Tests functions for generating paths and URLs to image styles.'),
'group' => t('Image')
);
}
function setUp() {
parent::setUp();
$this->style_name = 'style_foo';
// Create the directories for the styles.
$status = file_check_directory($d = file_directory_path() .'/styles/' . $this->style_name, FILE_CREATE_DIRECTORY);
$this->assertNotIdentical(FALSE, $status, t('Created the directory for the generated images for the test style.' ));
// Make two copies of the file...
$file = reset($this->drupalGetTestFiles('image'));
$this->image_without_generated = file_unmanaged_copy($file->filepath, NULL, FILE_EXISTS_RENAME);
$this->assertNotIdentical(FALSE, $this->image_without_generated, t('Created the without generated image file.'));
$this->image_with_generated = file_unmanaged_copy($file->filepath, NULL, FILE_EXISTS_RENAME);
$this->assertNotIdentical(FALSE, $this->image_with_generated, t('Created the with generated image file.'));
// and create a "generated" file for the one.
$status = file_unmanaged_copy($file->filepath, image_style_path($this->style_name, $this->image_with_generated), FILE_EXISTS_REPLACE);
$this->assertNotIdentical(FALSE, $status, t('Created a file where the generated image should be.'));
}
/**
* Test image_style_path().
*/
function testImageStylePath() {
$actual = image_style_path($this->style_name, $this->image_without_generated);
$expected = file_directory_path() . '/styles/' . $this->style_name . '/' . basename($this->image_without_generated);
$this->assertEqual($actual, $expected, t('Got the path for a file.'));
}
/**
* Test image_style_url().
*/
function testImageStyleUrl() {
// Test it with no generated file.
$actual = image_style_url($this->style_name, $this->image_without_generated);
$expected = url('image/generate/' . $this->style_name . '/' . $this->image_without_generated, array('absolute' => TRUE));
$this->assertEqual($actual, $expected, t('Got the generate URL for a non-existent file.'));
// Now test it with a generated file.
$actual = image_style_url($this->style_name, $this->image_with_generated);
$expected = file_create_url(image_style_path($this->style_name, $this->image_with_generated));
$this->assertEqual($actual, $expected, t('Got the download URL for an existing file.'));
}
/**
* Test image_style_generate_url().
*/
function testImageStyleGenerateUrl() {
// Test it with no generated file.
$actual = image_style_generate_url($this->style_name, $this->image_without_generated);
$expected = url('image/generate/' . $this->style_name . '/' . $this->image_without_generated, array('absolute' => TRUE));
$this->assertEqual($actual, $expected, t('Got the generate URL for a non-existent file.'));
// Now test it with a generated file.
$actual = image_style_generate_url($this->style_name, $this->image_with_generated);
$expected = file_create_url(image_style_path($this->style_name, $this->image_with_generated));
$this->assertEqual($actual, $expected, t('Got the download URL for an existing file.'));
}
}
/**
* Use the image_test.module's mock toolkit to ensure that the effects are
* properly passing parameters to the image toolkit.
*/
class ImageEffectsUnitTest extends ImageToolkitTestCase {
function getInfo() {
return array(
'name' => t('Image effects'),
'description' => t('Test that the image effects pass parameters to the toolkit correctly.'),
'group' => t('Image')
);
}
function setUp() {
parent::setUp('image_test');
module_load_include('inc', 'image', 'image.effects');
}
/**
* Test the image_effects() and image_effect_definitions() functions.
*/
function testEffects() {
$effects = image_effects();
$this->assertEqual(count($effects), 1, t("Found core's effect."));
$effect_definitions = image_effect_definitions();
$this->assertEqual(count($effect_definitions), 6, t("Found core's effects."));
}
/**
* Test the image_resize_effect() function.
*/
function testResizeEffect() {
$this->assertTrue(image_resize_effect($this->image, array('width' => 1, 'height' => 2)), t('Function returned the expected value.'));
$this->assertToolkitOperationsCalled(array('resize'));
// Check the parameters.
$calls = image_test_get_all_calls();
$this->assertEqual($calls['resize'][0][1], 1, t('Width was passed correctly'));
$this->assertEqual($calls['resize'][0][2], 2, t('Height was passed correctly'));
}
/**
* Test the image_scale_effect() function.
*/
function testScaleEffect() {
// @todo: need to test upscaling.
$this->assertTrue(image_scale_effect($this->image, array('width' => 10, 'height' => 10)), t('Function returned the expected value.'));
$this->assertToolkitOperationsCalled(array('resize'));
// Check the parameters.
$calls = image_test_get_all_calls();
$this->assertEqual($calls['resize'][0][1], 10, t('Width was passed correctly'));
$this->assertEqual($calls['resize'][0][2], 5, t('Height was based off aspect ratio and passed correctly'));
}
/**
* Test the image_crop_effect() function.
*/
function testCropEffect() {
// @todo should test the keyword offsets.
$this->assertTrue(image_crop_effect($this->image, array('anchor' => 'top-1', 'width' => 3, 'height' => 4)), t('Function returned the expected value.'));
$this->assertToolkitOperationsCalled(array('crop'));
// Check the parameters.
$calls = image_test_get_all_calls();
$this->assertEqual($calls['crop'][0][1], 0, t('X was passed correctly'));
$this->assertEqual($calls['crop'][0][2], 1, t('Y was passed correctly'));
$this->assertEqual($calls['crop'][0][3], 3, t('Width was passed correctly'));
$this->assertEqual($calls['crop'][0][4], 4, t('Height was passed correctly'));
}
/**
* Test the image_scale_and_crop_effect() function.
*/
function testScaleAndCropEffect() {
$this->assertTrue(image_scale_and_crop_effect($this->image, array('width' => 5, 'height' => 10)), t('Function returned the expected value.'));
$this->assertToolkitOperationsCalled(array('resize', 'crop'));
// Check the parameters.
$calls = image_test_get_all_calls();
$this->assertEqual($calls['crop'][0][1], 7.5, t('X was computed and passed correctly'));
$this->assertEqual($calls['crop'][0][2], 0, t('Y was computed and passed correctly'));
$this->assertEqual($calls['crop'][0][3], 5, t('Width was computed and passed correctly'));
$this->assertEqual($calls['crop'][0][4], 10, t('Height was computed and passed correctly'));
}
/**
* Test the image_desaturate_effect() function.
*/
function testDesaturateEffect() {
$this->assertTrue(image_desaturate_effect($this->image, array()), t('Function returned the expected value.'));
$this->assertToolkitOperationsCalled(array('desaturate'));
// Check the parameters.
$calls = image_test_get_all_calls();
$this->assertEqual(count($calls['desaturate'][0]), 1, t('Only the image was passed.'));
}
/**
* Test the image_rotate_effect() function.
*/
function testRotateEffect() {
// @todo: need to test with 'random' => TRUE
$this->assertTrue(image_rotate_effect($this->image, array('degrees' => 90, 'bgcolor' => '#fff')), t('Function returned the expected value.'));
$this->assertToolkitOperationsCalled(array('rotate'));
// Check the parameters.
$calls = image_test_get_all_calls();
$this->assertEqual($calls['rotate'][0][1], 90, t('Degrees were passed correctly'));
$this->assertEqual($calls['rotate'][0][2], 0xffffff, t('Background color was passed correctly'));
}
}
|