Spike PHPCoverage Details: upload.test

Line #FrequencySource Line
1 <?php
2 // $Id: upload.test,v 1.14 2008/04/04 14:19:33 rokZlender Exp $
3 
4 class UploadTestCase extends DrupalTestCase {
5   /**
6    * Implementation of getInfo().
7    */
8   function getInfo() {
9     return array(
101      'name' => t('Upload functionality'),
11       'description' => t('Check content uploaded to nodes.'),
12       'group' => t('Upload Tests'),
13     );
14   }
15 
16   function setUp() {
17     parent::setUp();
18 
19     $this->drupalModuleEnable('upload');
20   }
21 
22   /**
23    * Create node; upload files to node; and edit, and delete uploads.
24    */
25   function testNodeUpload() {
26     $admin_user = $this->drupalCreateUser(array('administer site configuration'));
27     $web_user = $this->drupalCreateUser(array('access content', 'edit any page content', 'upload files', 'view uploaded files'));
28 
29     $this->drupalLogin($admin_user);
30 
31     // Setup upload settings.
32     $edit = array();
33     $edit['upload_list_default'] = '1'; // Yes.
34     $edit['upload_extensions_default'] = 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp';
35     $edit['upload_uploadsize_default'] = '1';
36     $edit['upload_usersize_default'] = '1';
37     $this->drupalPost('admin/settings/uploads', $edit, t('Save configuration'));
38     $this->assertText('The configuration options have been saved.', 'Upload setting saved.');
39 
40     $this->drupalGet('logout');
41     $this->drupalLogin($web_user);
42 
43     // Create a node and attempt to attach files.
44     $node = $this->drupalCreateNode();
45     $files = array('README.txt', 'INSTALL.txt');
46 
47     $this->uploadFile($node, $files[0]);
48     $this->uploadFile($node, $files[1]);
49 
50     // Check to see that uploaded file is listed and actually accessible.
51     $this->assertText($files[0], $files[0] .' found on node.');
52     $this->assertText($files[1], $files[1] .' found on node.');
53 
54     $this->checkUploadedFile($files[0]);
55     $this->checkUploadedFile($files[1]);
56 
57     // Fetch db record and use fid to rename and delete file.
58     $upload = db_fetch_object(db_query('SELECT fid, description FROM {upload} WHERE nid = %d', array($node->nid)));
59     if ($upload) {
60       // Rename file.
61       $edit = array();
62       $edit['files['. $upload->fid .'][description]'] = $new_name = substr($upload->description, 1);
63       $this->drupalPost('node/'. $node->nid .'/edit', $edit, t('Save'));
64       $this->assertRaw(t('Page %title has been updated.', array('%title' => $node->title)), 'File renamed successfully.');
65 
66       $this->assertText($new_name, $new_name .' found on node.');
67       $this->assertNoText($upload->description, $upload->description .' not found on node.');
68 
69       // Delete a file.
70       $edit = array();
71       $edit['files['. $upload->fid .'][remove]'] = TRUE;
72       $this->drupalPost('node/'. $node->nid .'/edit', $edit, t('Save'));
73       $this->assertRaw(t('Page %title has been updated.', array('%title' => $node->title)), 'File deleted successfully.');
74 
75       $this->assertNoText($new_name, $new_name .' not found on node.');
76       $this->drupalGet(file_directory_path() .'/'. $upload->description);
77       $this->assertResponse(array(404), 'Uploaded '. $upload->description .' is not accessible.');
78     }
79     else {
80       $this->fail('File upload record not found in database.');
81     }
82   }
83 
84   /**
85    * Upload file to specified node.
86    *
87    * @param object $node Node object.
88    * @param string $filename Name of file to upload.
89    */
90   function uploadFile($node, $filename) {
91     $edit = array();
92     $edit['files[upload]'] = $this->getFilePath($filename);
93     $this->drupalPost('node/'. $node->nid .'/edit', $edit, t('Save'));
94     $this->assertRaw(t('Page %title has been updated.', array('%title' => $node->title)), 'File attached successfully.');
95   }
96 
97   /**
98    * Check that uploaded file is accessible and verify the contents against the original.
99    *
100    * @param string $filename Name of file to verifiy.
101    */
102   function checkUploadedFile($filename) {
103     $file = $this->getFilePath($filename);
104     $this->drupalGet(file_directory_path() .'/'. $filename);
105     $this->assertResponse(array(200), 'Uploaded '. $filename .' is accessible.');
106     $this->assertEqual(file_get_contents($file), $this->drupalGetContent(), 'Uploaded contents of '. $filename .' verified.');
107   }
108 
109   /**
110    * Get canonicalized absolute path to file located in the SimpleTest module.
111    *
112    * @param string $filename Name of file to get path for.
113    * @return string Absolute path.
114    */
115   function getFilePath($filename) {
116     return realpath(drupal_get_path('module', 'simpletest') .'/'. $filename);
117   }
118 }
119 
120 class UploadPictureTestCase extends DrupalTestCase {
121   function getInfo() {
122     return array(
1231      'name' => 'Upload user picture',
124       'description' => 'Assure that dimension check, extension check and image scaling work as designed.',
125       'group' => 'Upload Tests'
126     );
127   }
128 
129   /*
130    * Test if directories specified in settings exist in filesystem
131    */
132   function testDirectories() {
133     // test if filepath is proper
134     $file_dir = file_directory_path();
135     $picture_dir = variable_get('user_picture_path', 'pictures');
136     $file_check = file_check_directory($file_dir, FILE_CREATE_DIRECTORY, 'file_directory_path');
137     $picture_path = $file_dir .'/'.$picture_dir;
138 
139     $pic_check = file_check_directory($picture_path, FILE_CREATE_DIRECTORY, 'user_picture_path');
140     // check directories
141     //$this->assertTrue($file_check,"The directory $file_dir doesn't exist or cannot be created.");
142     //$this->assertTrue($pic_check,"The directory $picture_path doesn't exist or cannot be created.");
143     $this->_directory_test = is_writable($picture_path);
144     $this->assertTrue($this->_directory_test, "The directory $picture_path doesn't exist or is not writable. Further tests won't be made.");
145   }
146 
147   function testNoPicture() {
148     $old_pic_set = variable_get('user_pictures', 0);
149     variable_set('user_pictures', 1);
150 
151     /* Prepare a user to do the stuff */
152     $user = $this->drupalCreateUser(array('access content'));
153     $this->drupalLogin($user);
154 
155     // not a image
156     $img_path = realpath(drupal_get_path('module', 'simpletest'). "/tests/functional/upload.test");
157     $edit = array('files[picture_upload]' => $img_path);
158     $this->drupalPost('user/'.$user->uid.'/edit', $edit, t('Save'));
159     $this->assertRaw(t('The selected file %file could not be uploaded. Only JPEG, PNG and GIF images are allowed.', array('%file' => 'upload.test')), 'The uploaded file was not an image.');
160     variable_set('user_pictures', $old_pic_set);
161 
162     // do we have to check users roles?
163     // delete test user and roles
164 
165   }
166 
167   /*
168    * Do one test if ImageGDToolkit is installed
169    */
170 
171   /*
172    * Do the test:
173    *  GD Toolkit is installed
174    *  Picture has invalid dimension
175    *
176    * results: The image should be uploaded because ImageGDToolkit resizes the picture
177    */
178   function testWithGDinvalidDimension() {
179     if ($this->_directory_test)
180       if (image_get_toolkit()) {
181 
182         // PREPARE:
183         $old_pic_set = variable_get('user_pictures', 0);
184         variable_set('user_pictures', 1);
185 
186         /* Prepare a user to do the stuff */
187         $user = $this->drupalCreateUser(array('access content'));
188         $this->drupalLogin($user);
189 
190         // changing actual setting;
191         $old_dim = variable_get('user_picture_dimensions', '85x85');
192         $old_size = variable_get('user_picture_file_size', '30');
193         $img_path = realpath(drupal_get_path('module', 'simpletest'). "/tests/files/image-2.jpg");
194         $info = image_get_info($img_path);
195 
196         // set new variables;
197         $test_size = floor(filesize($img_path) / 1000) + 1;
198         $test_dim = ($info['width'] - 10) . 'x' . ($info['height'] - 10);
199         variable_set('user_picture_dimensions', $test_dim);
200         variable_set('user_picture_file_size', $test_size);
201 
202         // TEST:
203         $edit = array('files[picture_upload]' => $img_path);
204         $this->drupalPost('user/'.$user->uid.'/edit', $edit, t('Save'));
205         $picture_dir = variable_get('user_picture_path', 'pictures');
206         $picture = $picture_dir .'/picture-'.$user->uid.'.jpg';
207 
208         // get full url to the user's image
209         $picture_url = file_create_url($picture);
210         $picture_path = file_create_path($picture);
211 
212         // check if image is displayed in user's profile page
213         $this->assertRaw($picture_url, "Image is displayed in user's profile page");
214 
215         // check if file is located in proper directory
216         $this->assertTrue(is_file($picture_path), "File is located in proper directory");
217 
218         // RESTORING:
219         variable_set('user_picture_file_size', $old_size);
220         variable_set('user_picture_dimensions', $old_dim);
221 
222         variable_set('user_pictures', $old_pic_set);
223       }
224 
225   }
226 
227   /*
228    * Do the test:
229    *  GD Toolkit is installed
230    *  Picture has invalid size
231    *
232    * results: The image should be uploaded because ImageGDToolkit resizes the picture
233    */
234 
235   function testWithGDinvalidSize() {
236     if ($this->_directory_test)
237       if (image_get_toolkit()) {
238         // PREPARE:
239         $old_pic_set = variable_get('user_pictures', 0);
240         variable_set('user_pictures', 1);
241 
242         /* Prepare a user to do the stuff */
243         $user = $this->drupalCreateUser(array('access content'));
244         $this->drupalLogin($user);
245 
246         // changing actual setting;
247         $old_dim = variable_get('user_picture_dimensions', '85x85');
248         $old_size = variable_get('user_picture_file_size', '30');
249         $img_path = realpath(drupal_get_path('module', 'simpletest'). "/tests/files/image-2.jpg");
250         $info = image_get_info($img_path);
251         // set new variables;
252 
253         $test_dim = ($info['width'] + 10) . 'x' . ($info['height'] + 10);
254         $test_size = floor(filesize($img_path) / 1000) - 1;
255         variable_set('user_picture_dimensions', $test_dim);
256         variable_set('user_picture_file_size', $test_size);
257 
258         // TEST:
259         $edit = array('files[picture_upload]' => $img_path);
260         $this->drupalPost('user/'.$user->uid.'/edit', $edit, t('Save'));
261         $picture_dir = variable_get('user_picture_path', 'pictures');
262         $picture = $picture_dir .'/picture-'.$user->uid.'.jpg';
263 
264         // get full url to the user's image
265         $picture_url = file_create_url($picture);
266         $picture_path = file_create_path($picture);
267 
268         // check if image is displayed in user's profile page
269         $this->assertRaw($picture_url, "Image is displayed in user's profile page");
270 
271         // check if file is located in proper directory
272         $this->assertTrue(is_file($picture_path), "File is located in proper directory");
273 
274         // RESTORING:
275         variable_set('user_picture_file_size', $old_size);
276         variable_set('user_picture_dimensions', $old_dim);
277 
278         variable_set('user_pictures', $old_pic_set);
279       }
280   }
281 
282   /*
283    * Do the test:
284    *  GD Toolkit is not installed
285    *  Picture has invalid size
286    *
287    * results: The image shouldn't be uploaded
288    */
289    function testWithoutGDinvalidDimension() {
290     if ($this->_directory_test)
291       if (!image_get_toolkit()) {
292         // PREPARE:
293         $old_pic_set = variable_get('user_pictures', 0);
294         variable_set('user_pictures', 1);
295 
296         /* Prepare a user to do the stuff */
297         $user = $this->drupalCreateUser(array('access content'));
298         $this->drupalLogin($user);
299 
300         // changing actual setting;
301         $old_dim = variable_get('user_picture_dimensions', '85x85');
302         $old_size = variable_get('user_picture_file_size', '30');
303         $img_path = realpath(drupal_get_path('module', 'simpletest'). "/tests/files/image-2.jpg");
304         $info = image_get_info($img_path);
305         // set new variables;
306         $test_size = floor(filesize($img_path) / 1000) + 1;
307         $test_dim = ($info['width'] - 10) . 'x' . ($info['height'] - 10);
308         variable_set('user_picture_dimensions', $test_dim);
309         variable_set('user_picture_file_size', $test_size);
310 
311         // TEST:
312         $edit = array('picture' => $img_path);
313         $this->drupalPost('user/'.$user->uid.'/edit', $edit, t('Save'));
314         $text = t('The uploaded image is too large; the maximum dimensions are %dimensions pixels.', array('%dimensions' => variable_get('user_picture_dimensions', '85x85')));
315         $this->assertText($text, 'Checking response on invalid image (dimensions).');
316 
317         // check if file is not uploaded
318         $file_dir = variable_get('file_directory_path', 'files');
319         $picture_dir = variable_get('user_picture_path', 'pictures');
320         $pic_path = $file_dir .'/'.$picture_dir .'/picture-'.$user->uid.'.jpg';
321         $this->assertFalse(is_file($pic_path), "File is not uploaded");
322 
323         // restore variables;
324         variable_set('user_picture_file_size', $old_size);
325         variable_set('user_picture_dimensions', $old_dim);
326 
327         variable_set('user_pictures', $old_pic_set);
328       }
329    }
330 
331   /*
332    * Do the test:
333    *  GD Toolkit is not installed
334    *  Picture has invalid size
335    *
336    * results: The image shouldn't be uploaded
337    */
338    function testWithoutGDinvalidSize() {
339     if ($this->_directory_test)
340       if (!image_get_toolkit()) {
341         // PREPARE:
342         $old_pic_set = variable_get('user_pictures', 0);
343         variable_set('user_pictures', 1);
344 
345         /* Prepare a user to do the stuff */
346         $user = $this->drupalCreateUser(array('access content'));
347         $this->drupalLogin($user);
348 
349         // changing actual setting;
350         $old_dim = variable_get('user_picture_dimensions', '85x85');
351         $old_size = variable_get('user_picture_file_size', '30');
352         $img_path = realpath("modules/tests/image-2.jpg");
353         $info = image_get_info($img_path);
354         // invalid size
355         // restore one and set another
356         $test_dim = ($info['width'] + 10) . 'x' . ($info['height'] + 10);
357         $test_size = floor(filesize($img_path) / 1000) - 1;
358         variable_set('user_picture_dimensions', $test_dim);
359         variable_set('user_picture_file_size', $test_size);
360 
361         $edit = array('picture' => $img_path);
362         $this->drupalPost('user/'.$user->uid.'/edit', $edit, t('Save'));
363         $text = t('The uploaded image is too large; the maximum file size is %size kB.', array('%size' => variable_get('user_picture_file_size', '30')));
364         $this->assertText($text, 'Checking response on invalid image size.');
365 
366         // check if file is not uploaded
367         $file_dir = variable_get('file_directory_path', 'files');
368         $picture_dir = variable_get('user_picture_path', 'pictures');
369         $pic_path = $file_dir .'/'.$picture_dir .'/picture-'.$user->uid.'.jpg';
370         $this->assertFalse(is_file($pic_path), "File is not uploaded");
371         // restore variables;
372         variable_set('user_picture_file_size', $old_size);
373         variable_set('user_picture_dimensions', $old_dim);
374 
375         variable_set('user_pictures', $old_pic_set);
376       }
377   }
378 
379   /*
380    * Do the test:
381    *  Picture is valid (proper size and dimension)
382    *
383    * results: The image should be uploaded
384    */
385   function testPictureIsValid() {
386     if ($this->_directory_test) {
387       // PREPARE:
388       $old_pic_set = variable_get('user_pictures', 0);
389       variable_set('user_pictures', 1);
390 
391       /* Prepare a user to do the stuff */
392       $user = $this->drupalCreateUser(array('access content'));
393       $this->drupalLogin($user);
394 
395       // changing actual setting;
396       $old_dim = variable_get('user_picture_dimensions', '85x85');
397       $old_size = variable_get('user_picture_file_size', '30');
398       $img_path = realpath(drupal_get_path('module', 'simpletest'). "/tests/files/image-2.jpg");
399       $info = image_get_info($img_path);
400 
401       // valid size & dimensions
402       // restore one and set another
403       $test_dim = ($info['width'] + 10) . 'x' . ($info['height'] + 10);
404       $test_size = floor(filesize($img_path) / 1000) + 1;
405       variable_set('user_picture_dimensions', $test_dim);
406       variable_set('user_picture_file_size', $test_size);
407 
408       // TEST:
409       $edit = array('files[picture_upload]' => $img_path);
410       $this->drupalPost('user/'.$user->uid.'/edit', $edit, t('Save'));
411       $picture_dir = variable_get('user_picture_path', 'pictures');
412       $pic_path = file_directory_path() .'/'.$picture_dir .'/picture-'.$user->uid.'.jpg';
413 
414       // get full url to the user's image
415       $picture = file_create_url($pic_path);
416 
417       // check if image is displayed in user's profile page
418       $content = $this->drupalGetContent();
419 
420       $this->assertTrue(strpos($content, $picture), "Image is displayed in user's profile page");
421 
422       // check if file is located in proper directory
423       $this->assertTrue(is_file($pic_path), "File is located in proper directory");
424 
425       // RESTORING:
426       variable_set('user_picture_file_size', $old_size);
427       variable_set('user_picture_dimensions', $old_dim);
428 
429       variable_set('user_pictures', $old_pic_set);
430 
431       // DELETING UPLOADED PIC
432       file_delete($pic_path);
433     }
434   }
435 }