Projet

Général

Profil

Paste
Télécharger (5,54 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / media / modules / media_wysiwyg / media_wysiwyg.test @ 05237dd8

1
<?php
2

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

    
8
/**
9
 * Defines base class for media test cases.
10
 */
11
abstract class MediaWYSIWYGTestHelper extends DrupalWebTestCase {
12

    
13
  /**
14
   * Enable media and file entity modules for testing.
15
   */
16
  public function setUp() {
17
    $modules = func_get_args();
18
    if (isset($modules[0]) && is_array($modules[0])) {
19
      $modules = $modules[0];
20
    }
21
    $modules[] = 'media_wysiwyg';
22
    parent::setUp($modules);
23
  }
24

    
25
  /**
26
    * Generates markup to be inserted for a file.
27
    *
28
    * This is a PHP version of InsertMedia.insert() from js/wysiwyg-media.js.
29
    *
30
    * @param int $fid
31
    *   Drupal file id
32
    * @param int $count
33
    *   Quantity of markup to insert
34
    * @param array $attributes
35
    *   Extra attributes to insert.
36
    * @param array $fields
37
    *   Extra field values to insert.
38
    *
39
    * @return string
40
    *   Filter markup.
41
    */
42
  protected function generateJsonTokenMarkup($fid, $count = 1, array $attributes = array(), array $fields = array()) {
43
    $markup = '';
44
    // Merge default atttributes.
45
    $attributes += array(
46
      'height' => 100,
47
      'width' => 100,
48
      'classes' => 'media-element file_preview',
49
    );
50

    
51
    // Build the data that is used in a media tag.
52
    $data = array(
53
      'fid' => $fid,
54
      'type' => 'media',
55
      'view_mode' => 'preview',
56
      'attributes' => $attributes,
57
      'fields' => $fields,
58
    );
59

    
60
    // Create the file usage markup.
61
    $markup .= '<p>Intro paragraph</p>';
62
    for ($i = 1; $i <= $count; $i++) {
63
      $markup .= '<p>[[' . drupal_json_encode($data) . ']]</p>';
64
    }
65
    $markup .= '<p>Finish paragraph</p>';
66

    
67
    return $markup;
68
  }
69

    
70
  /**
71
   * Utility function to create a test node.
72
   *
73
   * @param int $fid
74
   *   Create the node with media markup in the body field
75
    * @param array $attributes
76
    *   Extra attributes to insert to the file.
77
    * @param array $fields
78
    *   Extra field values to insert.
79
   *
80
   * @return int
81
   *   Returns the node id
82
   */
83
  protected function createNode($fid = FALSE, array $attributes = array(), array $fields = array()) {
84
    $markup = '';
85
    if (! empty($fid)) {
86
      $markup = $this->generateJsonTokenMarkup($fid, 1, $attributes, $fields);
87
    }
88

    
89
    // Create an article node with file markup in the body field.
90
    $edit = array(
91
      'title' => $this->randomName(8),
92
      'body[und][0][value]' => $markup,
93
    );
94
    // Save the article node. First argument is the URL, then the value array
95
    // and the third is the label the button that should be "clicked".
96
    $this->drupalPost('node/add/article', $edit, t('Save'));
97

    
98
    // Get the article node that was saved by the unique title.
99
    $node = $this->drupalGetNodeByTitle($edit['title']);
100
    return $node->nid;
101
  }
102

    
103
}
104

    
105
/**
106
 * Defines base class for media_wysiwyg_view_mode test cases.
107
 */
108
class MediaWYSIWYGViewModeTestHelper extends MediaWYSIWYGTestHelper {
109
  function setUp() {
110
    parent::setUp();
111

    
112
    $web_user = $this->drupalCreateUser(array('access administration pages', 'administer file types', 'view files', 'use media wysiwyg'));
113
    $this->drupalLogin($web_user);
114
  }
115
}
116

    
117
/**
118
 * Test configuring view modes available on the format form.
119
 */
120
class FormatFormViewModesTest extends MediaWYSIWYGViewModeTestHelper {
121
  public static function getInfo() {
122
    return array(
123
      'name' => 'Format Form WYSIWYG View Modes',
124
      'description' => 'Test configuring view modes available on the format form.',
125
      'group' => 'Media WYSIWYG',
126
    );
127
  }
128

    
129
  function setUp() {
130
    parent::setUp();
131
  }
132

    
133
  /**
134
   * Configure format form view mode restrictions and ensure that they are followed.
135
   */
136
  function testAllowedFormatFormViewModes() {
137
    // Create an image file to test with.
138
    $files = $this->drupalGetTestFiles('image');
139
    $files[0]->status = FILE_STATUS_PERMANENT;
140
    $file = file_save($files[0]);
141
    $fid = $file->fid;
142

    
143
    // Teaser view mode should be selectable.
144
    $this->drupalGet('media/' . $fid . '/format-form');
145
    $this->assertResponse(200);
146
    $this->assertOption('edit-format', 'teaser');
147

    
148
    // Restrict the use of the default view mode.
149
    $this->drupalGet('admin/structure/file-types/manage/image/file-display/teaser');
150
    $this->assertResponse(200);
151
    $edit = array(
152
      'restrict_wysiwyg' => 1,
153
    );
154
    $this->drupalPost(NULL, $edit, t('Save configuration'));
155
    $this->assertResponse(200);
156

    
157
    // Teaser view mode should be restricted.
158
    $this->drupalGet('media/' . $fid . '/format-form');
159
    $this->assertResponse(200);
160
    $this->assertNoOption('edit-format', 'teaser');
161
  }
162

    
163
  /**
164
   * Assert that a select option in the current page exists.
165
   *
166
   * @param $id
167
   *   id of select field to assert.
168
   * @param $option
169
   *   Option to assert.
170
   */
171
  function assertOption($id, $option, $message = '', $group = 'Browser') {
172
    $options = $this->xpath('//select[@id=:id]/option[@value=:option]', array(':id' => $id, ':option' => $option));
173
    return $this->assertTrue(isset($options[0]), $message ? $message : t('Option @option for field @id exists.', array('@option' => $option, '@id' => $id)));
174
  }
175

    
176
  /**
177
   * Assert that a select option in the current page does not exist.
178
   *
179
   * @param $id
180
   *   id of select field to assert.
181
   * @param $option
182
   *   Option to assert.
183
   */
184
  function assertNoOption($id, $option, $message = '', $group = 'Browser') {
185
    $options = $this->xpath('//select[@id=:id]/option[@value=:option]', array(':id' => $id, ':option' => $option));
186
    return $this->assertFalse(isset($options[0]), $message ? $message : t('Option @option for field @id does not exist.', array('@option' => $option, '@id' => $id)));
187
  }
188

    
189
}