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 @ 5d12d676

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
   *
28
   */
29
  protected function setUp() {
30
    parent::setUp();
31
    $this->column_map = array(
32
      'views_test_name' => 'name',
33
    );
34
  }
35

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
180
    // Test a valid string.
181
    $view->result[0]->{$column_map_reversed['name']} = $random_name;
182
    $render = $view->field['name']->advanced_render($view->result[0]);
183
    $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.');
184

    
185
    // Test an empty string.
186
    $view->result[0]->{$column_map_reversed['name']} = "";
187
    $render = $view->field['name']->advanced_render($view->result[0]);
188
    $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.');
189

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
273
    $view->result[0]->{$column_map_reversed['name']} = "0";
274
    $render = $view->field['name']->advanced_render($view->result[0]);
275
    $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.');
276

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

    
282
    $view->result[0]->{$column_map_reversed['name']} = "";
283
    $view->field['name']->options['alter']['alter_text'] = TRUE;
284
    $alter_text = $view->field['name']->options['alter']['text'] = $this->randomName();
285
    $view->field['name']->options['hide_alter_empty'] = FALSE;
286
    $render = $view->field['name']->advanced_render($view->result[0]);
287
    $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.');
288

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

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

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

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

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

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

    
317
}