Projet

Général

Profil

Paste
Télécharger (15,3 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / tests / handlers / views_handler_field.test @ 4003efde

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of ViewsHandlerFieldTest.
6
 */
7

    
8
/**
9
 * Tests the generic field handler.
10
 *
11
 * @see views_handler_field
12
 */
13
class ViewsHandlerFieldTest extends ViewsSqlTest {
14

    
15
  /**
16
   *
17
   */
18
  public static function getInfo() {
19
    return array(
20
      'name' => 'Field',
21
      'description' => 'Test the core views_handler_field handler.',
22
      'group' => 'Views Handlers',
23
    );
24
  }
25

    
26
  /**
27
   * {@inheritdoc}
28
   */
29
  public function setUp(array $modules = array()) {
30
    parent::setUp($modules);
31

    
32
    $this->column_map = array(
33
      'views_test_name' => 'name',
34
    );
35
  }
36

    
37
  /**
38
   * Tests the hide if empty functionality.
39
   *
40
   * This tests alters the result to get easier and less coupled results.
41
   */
42
  public function testHideIfEmpty() {
43
    $view = $this->getBasicView();
44
    $view->init_display();
45
    $this->executeView($view);
46

    
47
    $column_map_reversed = array_flip($this->column_map);
48
    $view->row_index = 0;
49
    $random_name = $this->randomName();
50
    $random_value = $this->randomName();
51

    
52
    // Test when results are not rewritten and empty values are not hidden.
53
    $view->field['name']->options['hide_alter_empty'] = FALSE;
54
    $view->field['name']->options['hide_empty'] = FALSE;
55
    $view->field['name']->options['empty_zero'] = FALSE;
56

    
57
    // Test a valid string.
58
    $view->result[0]->{$column_map_reversed['name']} = $random_name;
59
    $render = $view->field['name']->advanced_render($view->result[0]);
60
    $this->assertIdentical($render, $random_name, 'By default, a string should not be treated as empty.');
61

    
62
    // Test an empty string.
63
    $view->result[0]->{$column_map_reversed['name']} = "";
64
    $render = $view->field['name']->advanced_render($view->result[0]);
65
    $this->assertIdentical($render, "", 'By default, "" should not be treated as empty.');
66

    
67
    // Test zero as an integer.
68
    $view->result[0]->{$column_map_reversed['name']} = 0;
69
    $render = $view->field['name']->advanced_render($view->result[0]);
70
    $this->assertIdentical($render, '0', 'By default, 0 should not be treated as empty.');
71

    
72
    // Test zero as a string.
73
    $view->result[0]->{$column_map_reversed['name']} = "0";
74
    $render = $view->field['name']->advanced_render($view->result[0]);
75
    $this->assertIdentical($render, "0", 'By default, "0" should not be treated as empty.');
76

    
77
    // Test when results are not rewritten and non-zero empty values are hidden.
78
    $view->field['name']->options['hide_alter_empty'] = TRUE;
79
    $view->field['name']->options['hide_empty'] = TRUE;
80
    $view->field['name']->options['empty_zero'] = FALSE;
81

    
82
    // Test a valid string.
83
    $view->result[0]->{$column_map_reversed['name']} = $random_name;
84
    $render = $view->field['name']->advanced_render($view->result[0]);
85
    $this->assertIdentical($render, $random_name, 'If hide_empty is checked, a string should not be treated as empty.');
86

    
87
    // Test an empty string.
88
    $view->result[0]->{$column_map_reversed['name']} = "";
89
    $render = $view->field['name']->advanced_render($view->result[0]);
90
    $this->assertIdentical($render, "", 'If hide_empty is checked, "" should be treated as empty.');
91

    
92
    // Test zero as an integer.
93
    $view->result[0]->{$column_map_reversed['name']} = 0;
94
    $render = $view->field['name']->advanced_render($view->result[0]);
95
    $this->assertIdentical($render, '0', 'If hide_empty is checked, but not empty_zero, 0 should not be treated as empty.');
96

    
97
    // Test zero as a string.
98
    $view->result[0]->{$column_map_reversed['name']} = "0";
99
    $render = $view->field['name']->advanced_render($view->result[0]);
100
    $this->assertIdentical($render, "0", 'If hide_empty is checked, but not empty_zero, "0" should not be treated as empty.');
101

    
102
    // Test when results are not rewritten and all empty values are hidden.
103
    $view->field['name']->options['hide_alter_empty'] = TRUE;
104
    $view->field['name']->options['hide_empty'] = TRUE;
105
    $view->field['name']->options['empty_zero'] = TRUE;
106

    
107
    // Test zero as an integer.
108
    $view->result[0]->{$column_map_reversed['name']} = 0;
109
    $render = $view->field['name']->advanced_render($view->result[0]);
110
    $this->assertIdentical($render, "", 'If hide_empty and empty_zero are checked, 0 should be treated as empty.');
111

    
112
    // Test zero as a string.
113
    $view->result[0]->{$column_map_reversed['name']} = "0";
114
    $render = $view->field['name']->advanced_render($view->result[0]);
115
    $this->assertIdentical($render, "", 'If hide_empty and empty_zero are checked, "0" should be treated as empty.');
116

    
117
    // Test when results are rewritten to a valid string and non-zero empty
118
    // results are hidden.
119
    $view->field['name']->options['hide_alter_empty'] = FALSE;
120
    $view->field['name']->options['hide_empty'] = TRUE;
121
    $view->field['name']->options['empty_zero'] = FALSE;
122
    $view->field['name']->options['alter']['alter_text'] = TRUE;
123
    $view->field['name']->options['alter']['text'] = $random_name;
124

    
125
    // Test a valid string.
126
    $view->result[0]->{$column_map_reversed['name']} = $random_value;
127
    $render = $view->field['name']->advanced_render($view->result[0]);
128
    $this->assertIdentical($render, $random_name, 'If the rewritten string is not empty, it should not be treated as empty.');
129

    
130
    // Test an empty string.
131
    $view->result[0]->{$column_map_reversed['name']} = "";
132
    $render = $view->field['name']->advanced_render($view->result[0]);
133
    $this->assertIdentical($render, $random_name, 'If the rewritten string is not empty, "" should not be treated as empty.');
134

    
135
    // Test zero as an integer.
136
    $view->result[0]->{$column_map_reversed['name']} = 0;
137
    $render = $view->field['name']->advanced_render($view->result[0]);
138
    $this->assertIdentical($render, $random_name, 'If the rewritten string is not empty, 0 should not be treated as empty.');
139

    
140
    // Test zero as a string.
141
    $view->result[0]->{$column_map_reversed['name']} = "0";
142
    $render = $view->field['name']->advanced_render($view->result[0]);
143
    $this->assertIdentical($render, $random_name, 'If the rewritten string is not empty, "0" should not be treated as empty.');
144

    
145
    // Test when results are rewritten to an empty string and non-zero empty
146
    // results are hidden.
147
    $view->field['name']->options['hide_alter_empty'] = TRUE;
148
    $view->field['name']->options['hide_empty'] = TRUE;
149
    $view->field['name']->options['empty_zero'] = FALSE;
150
    $view->field['name']->options['alter']['alter_text'] = TRUE;
151
    $view->field['name']->options['alter']['text'] = "";
152

    
153
    // Test a valid string.
154
    $view->result[0]->{$column_map_reversed['name']} = $random_name;
155
    $render = $view->field['name']->advanced_render($view->result[0]);
156
    $this->assertIdentical($render, $random_name, 'If the rewritten string is empty, it should not be treated as empty.');
157

    
158
    // Test an empty string.
159
    $view->result[0]->{$column_map_reversed['name']} = "";
160
    $render = $view->field['name']->advanced_render($view->result[0]);
161
    $this->assertIdentical($render, "", 'If the rewritten string is empty, "" should be treated as empty.');
162

    
163
    // Test zero as an integer.
164
    $view->result[0]->{$column_map_reversed['name']} = 0;
165
    $render = $view->field['name']->advanced_render($view->result[0]);
166
    $this->assertIdentical($render, '0', 'If the rewritten string is empty, 0 should not be treated as empty.');
167

    
168
    // Test zero as a string.
169
    $view->result[0]->{$column_map_reversed['name']} = "0";
170
    $render = $view->field['name']->advanced_render($view->result[0]);
171
    $this->assertIdentical($render, "0", 'If the rewritten string is empty, "0" should not be treated as empty.');
172

    
173
    // Test when results are rewritten to zero as a string and non-zero empty
174
    // results are hidden.
175
    $view->field['name']->options['hide_alter_empty'] = FALSE;
176
    $view->field['name']->options['hide_empty'] = TRUE;
177
    $view->field['name']->options['empty_zero'] = FALSE;
178
    $view->field['name']->options['alter']['alter_text'] = TRUE;
179
    $view->field['name']->options['alter']['text'] = "0";
180

    
181
    // Test a valid string.
182
    $view->result[0]->{$column_map_reversed['name']} = $random_name;
183
    $render = $view->field['name']->advanced_render($view->result[0]);
184
    $this->assertIdentical($render, "0", 'If the rewritten string is zero and empty_zero is not checked, the string rewritten as 0 should not be treated as empty.');
185

    
186
    // Test an empty string.
187
    $view->result[0]->{$column_map_reversed['name']} = "";
188
    $render = $view->field['name']->advanced_render($view->result[0]);
189
    $this->assertIdentical($render, "0", 'If the rewritten string is zero and empty_zero is not checked, "" rewritten as 0 should not be treated as empty.');
190

    
191
    // Test zero as an integer.
192
    $view->result[0]->{$column_map_reversed['name']} = 0;
193
    $render = $view->field['name']->advanced_render($view->result[0]);
194
    $this->assertIdentical($render, "0", 'If the rewritten string is zero and empty_zero is not checked, 0 should not be treated as empty.');
195

    
196
    // Test zero as a string.
197
    $view->result[0]->{$column_map_reversed['name']} = "0";
198
    $render = $view->field['name']->advanced_render($view->result[0]);
199
    $this->assertIdentical($render, "0", 'If the rewritten string is zero and empty_zero is not checked, "0" should not be treated as empty.');
200

    
201
    // Test when results are rewritten to a valid string and non-zero empty
202
    // results are hidden.
203
    $view->field['name']->options['hide_alter_empty'] = TRUE;
204
    $view->field['name']->options['hide_empty'] = TRUE;
205
    $view->field['name']->options['empty_zero'] = FALSE;
206
    $view->field['name']->options['alter']['alter_text'] = TRUE;
207
    $view->field['name']->options['alter']['text'] = $random_value;
208

    
209
    // Test a valid string.
210
    $view->result[0]->{$column_map_reversed['name']} = $random_name;
211
    $render = $view->field['name']->advanced_render($view->result[0]);
212
    $this->assertIdentical($render, $random_value, 'If the original and rewritten strings are valid, it should not be treated as empty.');
213

    
214
    // Test an empty string.
215
    $view->result[0]->{$column_map_reversed['name']} = "";
216
    $render = $view->field['name']->advanced_render($view->result[0]);
217
    $this->assertIdentical($render, "", 'If either the original or rewritten string is invalid, "" should be treated as empty.');
218

    
219
    // Test zero as an integer.
220
    $view->result[0]->{$column_map_reversed['name']} = 0;
221
    $render = $view->field['name']->advanced_render($view->result[0]);
222
    $this->assertIdentical($render, $random_value, 'If the original and rewritten strings are valid, 0 should not be treated as empty.');
223

    
224
    // Test zero as a string.
225
    $view->result[0]->{$column_map_reversed['name']} = "0";
226
    $render = $view->field['name']->advanced_render($view->result[0]);
227
    $this->assertIdentical($render, $random_value, 'If the original and rewritten strings are valid, "0" should not be treated as empty.');
228

    
229
    // Test when results are rewritten to zero as a string and all empty
230
    // original values and results are hidden.
231
    $view->field['name']->options['hide_alter_empty'] = TRUE;
232
    $view->field['name']->options['hide_empty'] = TRUE;
233
    $view->field['name']->options['empty_zero'] = TRUE;
234
    $view->field['name']->options['alter']['alter_text'] = TRUE;
235
    $view->field['name']->options['alter']['text'] = "0";
236

    
237
    // Test a valid string.
238
    $view->result[0]->{$column_map_reversed['name']} = $random_name;
239
    $render = $view->field['name']->advanced_render($view->result[0]);
240
    $this->assertIdentical($render, "", 'If the rewritten string is zero, it should be treated as empty.');
241

    
242
    // Test an empty string.
243
    $view->result[0]->{$column_map_reversed['name']} = "";
244
    $render = $view->field['name']->advanced_render($view->result[0]);
245
    $this->assertIdentical($render, "", 'If the rewritten string is zero, "" should be treated as empty.');
246

    
247
    // Test zero as an integer.
248
    $view->result[0]->{$column_map_reversed['name']} = 0;
249
    $render = $view->field['name']->advanced_render($view->result[0]);
250
    $this->assertIdentical($render, "", 'If the rewritten string is zero, 0 should not be treated as empty.');
251

    
252
    // Test zero as a string.
253
    $view->result[0]->{$column_map_reversed['name']} = "0";
254
    $render = $view->field['name']->advanced_render($view->result[0]);
255
    $this->assertIdentical($render, "", 'If the rewritten string is zero, "0" should not be treated as empty.');
256
  }
257

    
258
  /**
259
   * Tests the usage of the empty text.
260
   */
261
  public function testEmptyText() {
262
    $view = $this->getBasicView();
263
    $view->init_display();
264
    $this->executeView($view);
265

    
266
    $column_map_reversed = array_flip($this->column_map);
267
    $view->row_index = 0;
268

    
269
    $empty_text = $view->field['name']->options['empty'] = $this->randomName();
270
    $view->result[0]->{$column_map_reversed['name']} = "";
271
    $render = $view->field['name']->advanced_render($view->result[0]);
272
    $this->assertIdentical($render, $empty_text, 'If a field is empty, the empty text should be used for the output.');
273

    
274
    $view->result[0]->{$column_map_reversed['name']} = "0";
275
    $render = $view->field['name']->advanced_render($view->result[0]);
276
    $this->assertIdentical($render, "0", 'If a field is 0 and empty_zero is not checked, the empty text should not be used for the output.');
277

    
278
    $view->result[0]->{$column_map_reversed['name']} = "0";
279
    $view->field['name']->options['empty_zero'] = TRUE;
280
    $render = $view->field['name']->advanced_render($view->result[0]);
281
    $this->assertIdentical($render, $empty_text, 'If a field is 0 and empty_zero is checked, the empty text should be used for the output.');
282

    
283
    $view->result[0]->{$column_map_reversed['name']} = "";
284
    $view->field['name']->options['alter']['alter_text'] = TRUE;
285
    $alter_text = $view->field['name']->options['alter']['text'] = $this->randomName();
286
    $view->field['name']->options['hide_alter_empty'] = FALSE;
287
    $render = $view->field['name']->advanced_render($view->result[0]);
288
    $this->assertIdentical($render, $alter_text, 'If a field is empty, some rewrite text exists, but hide_alter_empty is not checked, render the rewrite text.');
289

    
290
    $view->field['name']->options['hide_alter_empty'] = TRUE;
291
    $render = $view->field['name']->advanced_render($view->result[0]);
292
    $this->assertIdentical($render, $empty_text, 'If a field is empty, some rewrite text exists, and hide_alter_empty is checked, use the empty text.');
293
  }
294

    
295
  /**
296
   * Tests views_handler_field::is_value_empty().
297
   */
298
  public function testIsValueEmpty() {
299
    $view = $this->getBasicView();
300
    $view->init_display();
301
    $view->init_handlers();
302
    $field = $view->field['name'];
303

    
304
    $this->assertFalse($field->is_value_empty("not empty", TRUE), 'A normal string is not empty.');
305
    $this->assertTrue($field->is_value_empty("not empty", TRUE, FALSE), 'A normal string which skips empty() can be seen as empty.');
306

    
307
    $this->assertTrue($field->is_value_empty("", TRUE), '"" is considered as empty.');
308

    
309
    $this->assertTrue($field->is_value_empty('0', TRUE), '"0" is considered as empty if empty_zero is TRUE.');
310
    $this->assertTrue($field->is_value_empty(0, TRUE), '0 is considered as empty if empty_zero is TRUE.');
311
    $this->assertFalse($field->is_value_empty('0', FALSE), '"0" is considered not as empty if empty_zero is FALSE.');
312
    $this->assertFalse($field->is_value_empty(0, FALSE), '0 is considered not as empty if empty_zero is FALSE.');
313

    
314
    $this->assertTrue($field->is_value_empty(NULL, TRUE, TRUE), 'Null should be always seen as empty, regardless of no_skip_empty.');
315
    $this->assertTrue($field->is_value_empty(NULL, TRUE, FALSE), 'Null should be always seen as empty, regardless of no_skip_empty.');
316
  }
317

    
318
}