summaryrefslogtreecommitdiff
path: root/modules/upload/upload.test
blob: 51f9cc81e321488f96300532ad451135c1f26aad (plain)
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
<?php
// $Id$

class UploadTestCase extends DrupalWebTestCase {
  /**
   * Implementation of getInfo().
   */
  function getInfo() {
    return array(
      'name' => t('Upload functionality'),
      'description' => t('Check content uploaded to nodes.'),
      'group' => t('Upload'),
    );
  }

  function setUp() {
    parent::setUp('upload');
  }

  /**
   * Create node; upload files to node; and edit, and delete uploads.
   */
  function testNodeUpload() {
    $admin_user = $this->drupalCreateUser(array('administer site configuration'));
    $web_user = $this->drupalCreateUser(array('access content', 'edit any page content', 'upload files', 'view uploaded files'));

    $this->drupalLogin($admin_user);

    // Setup upload settings.
    $edit = array();
    $edit['upload_list_default'] = '1'; // Yes.
    $edit['upload_extensions_default'] = 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp';
    $edit['upload_uploadsize_default'] = '1.5';
    $edit['upload_usersize_default'] = '1.5';
    $this->drupalPost('admin/settings/uploads', $edit, t('Save configuration'));
    $this->assertText('The configuration options have been saved.', 'Upload setting saved.');

    $this->drupalLogout();
    $this->drupalLogin($web_user);

    // Create a node and attempt to attach files.
    $node = $this->drupalCreateNode();
    $text_files = $this->drupalGetTestFiles('text');
    $files = array(current($text_files)->filename, next($text_files)->filename);

    $this->uploadFile($node, $files[0]);
    $this->uploadFile($node, $files[1]);

    // Check to see that uploaded file is listed and actually accessible.
    $this->assertText(basename($files[0]), basename($files[0]) . ' found on node.');
    $this->assertText(basename($files[1]), basename($files[1]) . ' found on node.');

    $this->checkUploadedFile(basename($files[0]));
    $this->checkUploadedFile(basename($files[1]));

    // Fetch db record and use fid to rename and delete file.
    $upload = db_fetch_object(db_query('SELECT fid, description FROM {upload} WHERE nid = %d', array($node->nid)));
    if ($upload) {
      // Rename file.
      $edit = array();
      $edit['files[' . $upload->fid . '][description]'] = $new_name = substr($upload->description, 1);
      $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
      $this->assertRaw(t('Page %title has been updated.', array('%title' => $node->title)), 'File renamed successfully.');

      $this->assertText($new_name, $new_name . ' found on node.');
      $this->assertNoText($upload->description, $upload->description . ' not found on node.');

      // Delete a file.
      $edit = array();
      $edit['files[' . $upload->fid . '][remove]'] = TRUE;
      $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
      $this->assertRaw(t('Page %title has been updated.', array('%title' => $node->title)), 'File deleted successfully.');

      $this->assertNoText($new_name, $new_name . ' not found on node.');
      $this->drupalGet(file_directory_path() . '/' . $upload->description);
      $this->assertResponse(array(404), 'Uploaded ' . $upload->description . ' is not accessible.');
    }
    else {
      $this->fail('File upload record not found in database.');
    }
  }

  /**
   * Ensure the the file filter works correctly by attempting to upload a non-allowed file extension.
   */
  function testFilesFilter() {
    $admin_user = $this->drupalCreateUser(array('administer site configuration'));
    $web_user = $this->drupalCreateUser(array('access content', 'edit any page content', 'upload files', 'view uploaded files'));

    $this->drupalLogin($admin_user);

    // Setup upload settings.
    $settings = array();
    $settings['upload_list'] = '1'; // Yes.
    $settings['upload_extensions'] = 'html';
    $settings['upload_uploadsize'] = '1';
    $settings['upload_usersize'] = '1';
    $this->setUploadSettings($settings, $this->getSimpletestRoleId($web_user));

    $this->drupalLogin($web_user);

    $node = $this->drupalCreateNode();
    $text_files = $this->drupalGetTestFiles('text');
    $html_files = $this->drupalGetTestFiles('html');
    $files = array(current($text_files)->filename, current($html_files)->filename);

    // Attempt to upload .txt file when .test is only extension allowed.
    $this->uploadFile($node, $files[0], FALSE);
    $this->assertRaw(t('The selected file %name could not be uploaded. Only files with the following extensions are allowed: %files-allowed.', array('%name' => basename($files[0]), '%files-allowed' => $settings['upload_extensions'])), 'File '. $files[0] . ' was not allowed to be uploaded');

    // Attempt to upload .test file when .test is only extension allowed.
    $this->uploadFile($node, $files[1]);
  }

  /**
   * Attempt to upload a file that is larger than the maxsize and see that it fails.
   */
  function testLimit() {
    $files = $this->drupalGetTestFiles('text', 1310720); // 1 MB.
    $file = current($files)->filename;

    $admin_user = $this->drupalCreateUser(array('administer site configuration'));
    $web_user = $this->drupalCreateUser(array('access content', 'edit any page content', 'upload files', 'view uploaded files'));

    $this->drupalLogin($admin_user);

    // Setup upload settings.
    $settings = array();
    $settings['upload_list'] = '1'; // Yes.
    $settings['upload_extensions'] = 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp';
    $settings['upload_uploadsize'] = '0.5';
    $settings['upload_usersize'] = '1.5';
    $this->setUploadSettings($settings, $this->getSimpletestRoleId($web_user));

    $this->drupalLogin($web_user);

    $node = $this->drupalCreateNode();

    // Attempt to upload file which is bigger than the maximum size of 0.5 MB.
    $this->uploadFile($node, $file, FALSE);

    $info = stat($file);
    $filename = basename($file);
    $filesize = format_size($info['size']);
    $maxsize = format_size(parse_size(($settings['upload_uploadsize'] * 1024) . 'KB')); // Won't parse decimals.
    $this->assertRaw(t('The selected file %name could not be uploaded. The file is %filesize exceeding the maximum file size of %maxsize.', array('%name' => $filename, '%filesize' => $filesize, '%maxsize' => $maxsize)), t('File upload was blocked since it was larger than maxsize.'));
  }

  function setUploadSettings($settings, $rid = NULL) {
    $edit = array();
    foreach ($settings as $key => $value) {
      $edit[$key . '_default'] = $value;
      if ($rid !== NULL && $key != 'upload_list' && $key != 'upload_max_resolution') {
        $edit[$key . '_' . $rid] = $value;
      }
    }
    $this->drupalPost('admin/settings/uploads', $edit, 'Save configuration');
    $this->assertText('The configuration options have been saved.', 'Upload setting saved.');
  }

  /**
   * Upload file to specified node.
   *
   * @param object $node Node object.
   * @param string $filename Name of file to upload.
   * @param boolean $assert Assert that the node was successfully updated.
   */
  function uploadFile($node, $filename, $assert = TRUE) {
    $edit = array();
    $edit['files[upload]'] = $filename; //edit-upload
    $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
    if ($assert) {
      $this->assertRaw(t('Page %title has been updated.', array('%title' => $node->title)), 'File attached successfully.');
    }
  }

  /**
   * Check that uploaded file is accessible and verify the contents against the original.
   *
   * @param string $filename Name of file to verifiy.
   */
  function checkUploadedFile($filename) {
    $file = realpath(file_directory_path() . '/' . $filename);
    $this->drupalGet(file_directory_path() . '/' . $filename);
    $this->assertResponse(array(200), 'Uploaded ' . $filename . ' is accessible.');
    $this->assertEqual(file_get_contents($file), $this->drupalGetContent(), 'Uploaded contents of ' . $filename . ' verified.');
  }

  /**
   * Get the role id of the 'simpletest' role associated with a SimpleTest test user.
   *
   * @param object $user User object.
   * @return interger SimpleTest role id.
   */
  function getSimpletestRoleId($user) {
    foreach ($user->roles as $rid => $role) {
      if (strpos($role, 'simpletest') !== FALSE) {
        return $rid;
      }
    }
    return NULL;
  }
}