Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views_data_export / tests / csv_export.test @ 13755f8d

1
<?php
2

    
3
class CSVExportViewsDataExportTests extends ViewsDataExportSimpleExportTest {
4

    
5
  protected $profile = 'testing';
6

    
7
  public static function getInfo() {
8
    return array(
9
      'name' => 'CSV Export',
10
      'description' => 'Various tests for exporting to CSV.',
11
      'group' => 'Views Data Export',
12
    );
13
  }
14

    
15
  protected $vde_export_type = 'CSV';
16

    
17
  protected function getStylePluginName() {
18
    return 'views_data_export_csv';
19
  }
20

    
21
  protected function getExportView($path = 'vde_test') {
22
    // Create the basic view.
23
    $view = $this->getBasicExportView();
24

    
25
    $display = $view->new_display('views_data_export', 'Data export', 'vde_test');
26
    $display->override_option('style_plugin', $this->getStylePluginName());
27
    $display->override_option('path', $path);
28

    
29
    $expected = '"ID","Name","Age"
30
"1","John","25"
31
"2","George","27"
32
"3","Ringo","28"
33
"4","Paul","26"
34
"5","Meredith","30"';
35

    
36
    return array(&$view, $expected);
37
  }
38

    
39
  /**
40
   * Test to ensure that HTML tags are kept in CSV files when requested.
41
   */
42
  protected function testKeepHTML() {
43
    $view = $this->getBasicExportView();
44

    
45
    $display = $view->display['default']->handler;
46

    
47
    $display->override_option('fields', array(
48
      'id' => array(
49
        'id' => 'id',
50
        'table' => 'views_test',
51
        'field' => 'id',
52
        'relationship' => 'none',
53
        // Add a label to include HTML
54
        'label' => '<strong id="id">ID</strong>',
55
      ),
56
      'name' => array(
57
        'id' => 'name',
58
        'table' => 'views_test',
59
        'field' => 'name',
60
        'relationship' => 'none',
61
        // Alter this field to include HTML.
62
        'alter' => array(
63
          'alter_text' => TRUE,
64
          'text' => '<em>[name]</em>',
65
        ),
66
      ),
67
      'age' => array(
68
        'id' => 'age',
69
        'table' => 'views_test',
70
        'field' => 'age',
71
        'relationship' => 'none',
72
      ),
73
    ));
74

    
75
    $style_options = array(
76
      'keep_html' => TRUE,
77
    );
78

    
79
    $expected = '"<strong id=""id"">ID</strong>","Name","Age"
80
"1","<em>John</em>","25"
81
"2","<em>George</em>","27"
82
"3","<em>Ringo</em>","28"
83
"4","<em>Paul</em>","26"
84
"5","<em>Meredith</em>","30"';
85

    
86
    $message = 'Keep HTML test in ' . $this->vde_export_type . ' export matched expected output.';
87

    
88
    $this->executeAndCompareGivenView($view, $expected, $message, $style_options);
89

    
90

    
91
    // And now make sure that HTML tags are stripped correctly.
92
    $style_options = array(
93
      'keep_html' => FALSE,
94
    );
95

    
96
    $expected = '"ID","Name","Age"
97
"1","John","25"
98
"2","George","27"
99
"3","Ringo","28"
100
"4","Paul","26"
101
"5","Meredith","30"';
102

    
103
    $message = 'Keep HTML reverse test in ' . $this->vde_export_type . ' export matched expected output.';
104

    
105
    $this->executeAndCompareGivenView($view, $expected, $message, $style_options);
106
  }
107

    
108
  /**
109
   * Test to ensure that HTML tags are kept in CSV files when requested.
110
   */
111
  protected function testTrimFields() {
112
    $view = $this->getBasicExportView();
113

    
114
    $display = $view->display['default']->handler;
115

    
116
    $display->override_option('fields', array(
117
      'id' => array(
118
        'id' => 'id',
119
        'table' => 'views_test',
120
        'field' => 'id',
121
        'relationship' => 'none',
122
        'label' => 'ID',
123
      ),
124
      'name' => array(
125
        'id' => 'name',
126
        'table' => 'views_test',
127
        'field' => 'name',
128
        'relationship' => 'none',
129
        // Alter this field to include HTML.
130
        'alter' => array(
131
          'alter_text' => TRUE,
132
          'text' => ' [name]  ',
133
        ),
134
      ),
135
      'age' => array(
136
        'id' => 'age',
137
        'table' => 'views_test',
138
        'field' => 'age',
139
        'relationship' => 'none',
140
      ),
141
    ));
142

    
143
    $style_options = array(
144
      'trim' => FALSE,
145
    );
146

    
147
    $expected = '"ID","Name","Age"
148
"1"," John  ","25"
149
"2"," George  ","27"
150
"3"," Ringo  ","28"
151
"4"," Paul  ","26"
152
"5"," Meredith  ","30"';
153

    
154
    $message = 'Trim reverse test in ' . $this->vde_export_type . ' export matched expected output.';
155

    
156
    $this->executeAndCompareGivenView($view, $expected, $message, $style_options);
157

    
158
    // And now make sure that trimming works as expected.
159
    $style_options = array(
160
      'trim' => TRUE,
161
    );
162

    
163
    $expected = '"ID","Name","Age"
164
"1","John","25"
165
"2","George","27"
166
"3","Ringo","28"
167
"4","Paul","26"
168
"5","Meredith","30"';
169

    
170
    $message = 'Trim test in ' . $this->vde_export_type . ' export matched expected output.';
171

    
172
    $this->executeAndCompareGivenView($view, $expected, $message, $style_options);
173
  }
174

    
175
  /**
176
   * Test to ensure that Encoding options work.
177
   */
178
  protected function testIconvEncoding() {
179
    $view = $this->getBasicExportView();
180

    
181
    db_update('views_test')
182
      ->fields(array('name' => 'Jo€hn'))
183
      ->condition('name', 'John')
184
      ->execute();
185

    
186
    $encodings = array(
187
      'ISO-8859-1//TRANSLIT' => 'EUR',
188
      'utf8_decode' => '?',
189
    );
190

    
191
    foreach ($encodings as $encoding => $conversion) {
192

    
193
      $style_options = array(
194
        'encoding' => $encoding,
195
      );
196

    
197
      $expected = '"ID","Name","Age"
198
"1","Jo' . $conversion . 'hn","25"
199
"2","George","27"
200
"3","Ringo","28"
201
"4","Paul","26"
202
"5","Meredith","30"';
203

    
204
      $message = 'Character encoding ' . $encoding . ' worked correctly.';
205

    
206
      $this->executeAndCompareGivenView($view, $expected, $message, $style_options);
207
    }
208

    
209
  }
210

    
211
}