Projet

Général

Profil

Paste
Télécharger (13,4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / media / modules / media_internet / tests / media_internet.test @ e4215af7

1
<?php
2

    
3
/**
4
 * @file
5
 * Tests for media_internet.module.
6
 */
7

    
8
/**
9
 * Provides methods specifically for testing Media Internet module's remote media handling.
10
 */
11
class MediaInternetTestHelper extends DrupalWebTestCase {
12
  function setUp() {
13
    // Since this is a base class for many test cases, support the same
14
    // flexibility that DrupalWebTestCase::setUp() has for the modules to be
15
    // passed in as either an array or a variable number of string arguments.
16
    $modules = func_get_args();
17
    if (isset($modules[0]) && is_array($modules[0])) {
18
      $modules = $modules[0];
19
    }
20
    $modules[] = 'media_internet';
21
    parent::setUp($modules);
22
  }
23

    
24
  /**
25
   * Retrieves a sample file of the specified type.
26
   */
27
  function getTestFile($type_name, $size = NULL) {
28
    // Get a file to upload.
29
    $file = current($this->drupalGetTestFiles($type_name, $size));
30

    
31
    // Add a filesize property to files as would be read by file_load().
32
    $file->filesize = filesize($file->uri);
33

    
34
    return $file;
35
  }
36

    
37
  /**
38
   * Retrieves the fid of the last inserted file.
39
   */
40
  function getLastFileId() {
41
    return (int) db_query('SELECT MAX(fid) FROM {file_managed}')->fetchField();
42
  }
43

    
44
  /**
45
   * Get a file from the database based on its filename.
46
   *
47
   * @param $filename
48
   *   A file filename, usually generated by $this->randomName().
49
   * @param $reset
50
   *   (optional) Whether to reset the internal file_load() cache.
51
   *
52
   * @return
53
   *   A file object matching $filename.
54
   */
55
  function getFileByFilename($filename, $reset = FALSE) {
56
    $files = file_load_multiple(array(), array('filename' => $filename), $reset);
57
    // Load the first file returned from the database.
58
    $returned_file = reset($files);
59
    return $returned_file;
60
  }
61

    
62
  protected function createFileType($overrides = array()) {
63
    $type = new stdClass();
64
    $type->type = 'test';
65
    $type->label = "Test";
66
    $type->description = '';
67
    $type->mimetypes = array('image/jpeg', 'image/gif', 'image/png', 'image/tiff');
68

    
69
    foreach ($overrides as $k => $v) {
70
      $type->$k = $v;
71
    }
72

    
73
    file_type_save($type);
74
    return $type;
75
  }
76
}
77

    
78
/**
79
 * Tests the media browser 'Web' tab.
80
 */
81
class MediaInternetBrowserWebTabTestCase extends MediaInternetTestHelper {
82
  public static function getInfo() {
83
    return array(
84
      'name' => 'Media browser web tab test',
85
      'description' => 'Tests the media browser web tab.',
86
      'group' => 'Media Internet',
87
    );
88
  }
89

    
90
  function setUp() {
91
    parent::setUp();
92

    
93
    $web_user = $this->drupalCreateUser(array('access media browser', 'add media from remote sources'));
94
    $this->drupalLogin($web_user);
95
  }
96

    
97
  /**
98
   * Tests that the views sorting works on the media browser 'Library' tab.
99
   */
100
  function testMediaBrowserWebTab() {
101
    // Load only the 'Library' tab of the media browser.
102
    $options = array(
103
      'query' => array(
104
        'enabledPlugins' => array(
105
          'media_internet' => 'media_internet',
106
        ),
107
      ),
108
    );
109

    
110
    $this->drupalGet('media/browser', $options);
111
    $this->assertResponse(200);
112

    
113
    // Check that the web tab is available and has an 'embed code' field.
114
    $this->assertRaw(t('Web'), t('The web tab was found.'));
115
    $this->assertFieldByName('embed_code', '', t('Embed code form field found.'));
116
  }
117
}
118

    
119
/**
120
 * Test the default MediaInternetFileHandler provider.
121
 */
122
class MediaInternetRemoteFileTestCase extends MediaInternetTestHelper {
123
  public static function getInfo() {
124
    return array(
125
      'name' => 'Remote media file handler provider',
126
      'description' => 'Test the default remote file handler provider.',
127
      'group' => 'Media Internet',
128
    );
129
  }
130

    
131
  function setUp() {
132
    parent::setUp();
133

    
134
    // Disable the private file system which is automatically enabled by
135
    // DrupalTestCase so we can test the upload wizard correctly.
136
    variable_del('file_private_path');
137

    
138
    $web_user = $this->drupalCreateUser(array('create files', 'add media from remote sources'));
139
    $this->drupalLogin($web_user);
140
  }
141

    
142
  /**
143
   * Tests the default remote file handler.
144
   */
145
  function testRemoteFileHandling() {
146
    // Step 1: Add a basic document file by providing a URL to the file.
147
    $edit = array();
148
    $edit['embed_code'] = file_create_url('README.txt');
149
    $this->drupalPost('file/add/web', $edit, t('Next'));
150

    
151
    // Check that the file exists in the database.
152
    $fid = $this->getLastFileId();
153
    $file = file_load($fid);
154
    $this->assertTrue($file, t('File found in database.'));
155

    
156
    // Check that the video file has been uploaded.
157
    $this->assertRaw(t('!type %name was uploaded.', array('!type' => 'Document', '%name' => $file->filename)), t('Document file uploaded.'));
158
  }
159
}
160

    
161
/**
162
 * Tests custom media provider APIs.
163
 */
164
class MediaInternetProviderTestCase extends MediaInternetTestHelper {
165
  public static function getInfo() {
166
    return array(
167
      'name' => 'Custom media provider test',
168
      'description' => 'Tests the custom media provider APIs.',
169
      'group' => 'Media Internet',
170
    );
171
  }
172

    
173
  function setUp() {
174
    parent::setUp('media_internet_test');
175

    
176
    // Disable the private file system which is automatically enabled by
177
    // DrupalTestCase so we can test the upload wizard correctly.
178
    variable_del('file_private_path');
179

    
180
    // Enable media_internet_test.module's hook_media_internet_providers()
181
    // implementation.
182
    variable_set('media_internet_test_media_internet_providers', TRUE);
183

    
184
    $web_user = $this->drupalCreateUser(array('create files', 'view own private files', 'add media from remote sources'));
185
    $this->drupalLogin($web_user);
186
  }
187

    
188
  /**
189
   * Test the basic file upload wizard functionality.
190
   */
191
  function testMediaInternetCustomProviderWizardBasic() {
192
    $this->drupalGet('file/add/web');
193
    $this->assertResponse(200);
194

    
195
    // Check that the provider is listed as supported.
196
    $this->assertRaw(t('Supported internet media providers: !providers.', array('!providers' => '<strong>' . 'Media Internet Test' . '</strong>')), t('The example media provider is enabled.'));
197

    
198
    // Enable media_internet_test.module's
199
    // hook_media_browser_plugin_info_alter_alter() implementation and ensure it
200
    // is working as designed.
201
    variable_set('media_internet_test_media_internet_providers_alter', TRUE);
202

    
203
    $this->drupalGet('file/add/web');
204
    $this->assertRaw(t('Supported internet media providers: !providers.', array('!providers' => '<strong>' . 'Altered provider title' . '</strong>')), t('The example media provider was successfully altered.'));
205

    
206
    // Step 1: Upload a basic video file.
207
    $edit = array();
208
    $edit['embed_code'] = 'http://www.example.com/video/123';
209
    $this->drupalPost('file/add/web', $edit, t('Next'));
210

    
211
    // Check that the file exists in the database.
212
    $fid = $this->getLastFileId();
213
    $file = file_load($fid);
214
    $this->assertTrue($file, t('File found in database.'));
215

    
216
    // Check that the video file has been uploaded.
217
    $this->assertRaw(t('!type %name was uploaded.', array('!type' => 'Video', '%name' => $file->filename)), t('Video file uploaded.'));
218
  }
219

    
220
  /**
221
   * Test the file upload wizard type step.
222
   */
223
  function testMediaInternetCustomProviderWizardTypes() {
224
    // Create multiple file types with the same mime types.
225
    $this->createFileType(array('type' => 'video1', 'label' => 'Video 1', 'mimetypes' => array('video/mediainternettest')));
226
    $this->createFileType(array('type' => 'video2', 'label' => 'Video 2', 'mimetypes' => array('video/mediainternettest')));
227

    
228
    // Step 1: Upload a basic video file.
229
    $edit = array();
230
    $edit['embed_code'] = 'http://www.example.com/video/123';
231
    $this->drupalPost('file/add/web', $edit, t('Next'));
232

    
233
    // Step 2: File type selection.
234
    $edit = array();
235
    $edit['type'] = 'video2';
236
    $this->drupalPost(NULL, $edit, t('Next'));
237

    
238
    // Check that the file exists in the database.
239
    $fid = $this->getLastFileId();
240
    $file = file_load($fid);
241
    $this->assertTrue($file, t('File found in database.'));
242

    
243
    // Check that the video file has been uploaded.
244
    $this->assertRaw(t('!type %name was uploaded.', array('!type' => 'Video 2', '%name' => $file->filename)), t('Video 2 file uploaded.'));
245
  }
246

    
247
  /**
248
   * Test the file upload wizard scheme step.
249
   */
250
  function testMediaInternetCustomProviderWizardSchemes() {
251
    // Enable the private file system.
252
    variable_set('file_private_path', $this->private_files_directory);
253

    
254
    // Step 1: Upload a basic video file.
255
    $edit = array();
256
    $edit['embed_code'] = 'http://www.example.com/video/123';
257
    $this->drupalPost('file/add/web', $edit, t('Next'));
258

    
259
    // Step 3: Users should not be able to select a scheme for files with
260
    // read-only stream wrappers.
261
    $this->assertNoFieldByName('scheme');
262

    
263
    // Check that the file exists in the database.
264
    $fid = $this->getLastFileId();
265
    $file = file_load($fid);
266
    $this->assertTrue($file, t('File found in database.'));
267

    
268
    // Check that the video file has been uploaded.
269
    $this->assertRaw(t('!type %name was uploaded.', array('!type' => 'Video', '%name' => $file->filename)), t('Video file uploaded.'));
270
  }
271

    
272
  /**
273
   * Test the file upload wizard field step.
274
   */
275
  function testMediaInternetCustomProviderWizardFields() {
276
    $filename = $this->randomName();
277

    
278
    // Add a text field to the video file type.
279
    $field_name = drupal_strtolower($this->randomName() . '_field_name');
280
    $field = array('field_name' => $field_name, 'type' => 'text');
281
    field_create_field($field);
282
    $instance = array(
283
      'field_name' => $field_name,
284
      'entity_type' => 'file',
285
      'bundle' => 'video',
286
      'label' => $this->randomName() . '_label',
287
    );
288
    field_create_instance($instance);
289

    
290
    // Step 1: Upload a basic video file.
291
    $edit = array();
292
    $edit['embed_code'] = 'http://www.example.com/video/123';
293
    $this->drupalPost('file/add/web', $edit, t('Next'));
294

    
295
    // Step 4: Attached fields.
296
    $edit = array();
297
    $edit['filename'] = $filename;
298
    $edit[$field_name . '[' . LANGUAGE_NONE . '][0][value]'] = $this->randomName();
299
    $this->drupalPost(NULL, $edit, t('Save'));
300

    
301
    // Check that the file exists in the database.
302
    $fid = $this->getLastFileId();
303
    $file = file_load($fid);
304
    $this->assertTrue($file, t('File found in database.'));
305

    
306
    // Check that the video file has been uploaded.
307
    $this->assertRaw(t('!type %name was uploaded.', array('!type' => 'Video', '%name' => $filename)), t('Video file uploaded.'));
308
  }
309

    
310
  /**
311
   * Test skipping each of the file upload wizard steps.
312
   */
313
  function testMediaInternetCustomProviderWizardStepSkipping() {
314
    $filename = $this->randomName();
315

    
316
    // Ensure that the file is affected by every step.
317
    variable_set('file_private_path', $this->private_files_directory);
318

    
319
    $this->createFileType(array('type' => 'video1', 'label' => 'Video 1', 'mimetypes' => array('video/mediainternettest')));
320
    $this->createFileType(array('type' => 'video2', 'label' => 'Video 2', 'mimetypes' => array('video/mediainternettest')));
321

    
322
    $field_name = drupal_strtolower($this->randomName() . '_field_name');
323
    $field = array('field_name' => $field_name, 'type' => 'text');
324
    field_create_field($field);
325
    $instance = array(
326
      'field_name' => $field_name,
327
      'entity_type' => 'file',
328
      'bundle' => 'video2',
329
      'label' => $this->randomName() . '_label',
330
    );
331
    field_create_instance($instance);
332

    
333
    // Test skipping each upload wizard step.
334
    foreach (array('types', 'schemes', 'fields') as $step) {
335
      // Step to skip.
336
      switch ($step) {
337
        case 'types':
338
          variable_set('file_entity_file_upload_wizard_skip_file_type', TRUE);
339
          break;
340
        case 'schemes':
341
          variable_set('file_entity_file_upload_wizard_skip_scheme', TRUE);
342
          break;
343
        case 'fields':
344
          variable_set('file_entity_file_upload_wizard_skip_fields', TRUE);
345
          break;
346
      }
347

    
348
      // Step 1: Upload a basic video file.
349
      $edit = array();
350
      $edit['embed_code'] = 'http://www.example.com/video/123';
351
      $this->drupalPost('file/add/web', $edit, t('Next'));
352

    
353
      // Step 2: File type selection.
354
      if ($step != 'types') {
355
        $edit = array();
356
        $edit['type'] = 'video2';
357
        $this->drupalPost(NULL, $edit, t('Next'));
358
      }
359

    
360
      // Step 3: Users should not be able to select a scheme for files with
361
      // read-only stream wrappers.
362
      $this->assertNoFieldByName('scheme');
363

    
364
      // Step 4: Attached fields.
365
      if ($step != 'fields') {
366
        // Skipping file type selection essentially skips this step as well
367
        // because the file will not be assigned a type so no fields will be
368
        // available.
369
        if ($step != 'types') {
370
          $edit = array();
371
          $edit['filename'] = $filename;
372
          $edit[$field_name . '[' . LANGUAGE_NONE . '][0][value]'] = $this->randomName();
373
          $this->drupalPost(NULL, $edit, t('Save'));
374
        }
375
      }
376

    
377
      // Check that the file exists in the database.
378
      $fid = $this->getLastFileId();
379
      $file = file_load($fid);
380
      $this->assertTrue($file, t('File found in database.'));
381

    
382
      // Determine the file's file type.
383
      $type = file_type_load($file->type);
384

    
385
      // Check that the video file has been uploaded.
386
      $this->assertRaw(t('!type %name was uploaded.', array('!type' => $type->label, '%name' => $file->filename)), t('Video file uploaded.'));
387

    
388
      // Reset 'skip' variables.
389
      variable_del('file_entity_file_upload_wizard_skip_file_type');
390
      variable_del('file_entity_file_upload_wizard_skip_scheme');
391
      variable_del('file_entity_file_upload_wizard_skip_fields');
392
    }
393
  }
394
}