Projet

Général

Profil

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

root / drupal7 / sites / all / modules / media / modules / media_wysiwyg / media_wysiwyg.test @ e4215af7

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

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

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

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

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

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

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

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

    
190
}