Project

General

Profile

Paste
Download (2.62 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / media / modules / media_wysiwyg / media_wysiwyg.test @ 1f623f01

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
    for ($i = 1; $i <= $count; $i++) {
62
      $markup .= '<p>[[' . drupal_json_encode($data) . ']]</p>';
63
    }
64

    
65
    return $markup;
66
  }
67

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

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

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

    
101
}