commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / views / tests / handlers / views_handler_field.test
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 public static function getInfo() {
15 return array(
16 'name' => 'Field',
17 'description' => 'Test the core views_handler_field handler.',
18 'group' => 'Views Handlers',
19 );
20 }
21
22 protected function setUp() {
23 parent::setUp();
24 $this->column_map = array(
25 'views_test_name' => 'name',
26 );
27 }
28
29 function testEmpty() {
30 $this->_testHideIfEmpty();
31 $this->_testEmptyText();
32 }
33
34 /**
35 * Tests the hide if empty functionality.
36 *
37 * This tests alters the result to get easier and less coupled results.
38 */
39 function _testHideIfEmpty() {
40 $view = $this->getBasicView();
41 $view->init_display();
42 $this->executeView($view);
43
44 $column_map_reversed = array_flip($this->column_map);
45 $view->row_index = 0;
46 $random_name = $this->randomName();
47 $random_value = $this->randomName();
48
49 // Test when results are not rewritten and empty values are not hidden.
50 $view->field['name']->options['hide_alter_empty'] = FALSE;
51 $view->field['name']->options['hide_empty'] = FALSE;
52 $view->field['name']->options['empty_zero'] = FALSE;
53
54 // Test a valid string.
55 $view->result[0]->{$column_map_reversed['name']} = $random_name;
56 $render = $view->field['name']->advanced_render($view->result[0]);
57 $this->assertIdentical($render, $random_name, 'By default, a string should not be treated as empty.');
58
59 // Test an empty string.
60 $view->result[0]->{$column_map_reversed['name']} = "";
61 $render = $view->field['name']->advanced_render($view->result[0]);
62 $this->assertIdentical($render, "", 'By default, "" should not be treated as empty.');
63
64 // Test zero as an integer.
65 $view->result[0]->{$column_map_reversed['name']} = 0;
66 $render = $view->field['name']->advanced_render($view->result[0]);
67 $this->assertIdentical($render, '0', 'By default, 0 should not be treated as empty.');
68
69 // Test zero as a string.
70 $view->result[0]->{$column_map_reversed['name']} = "0";
71 $render = $view->field['name']->advanced_render($view->result[0]);
72 $this->assertIdentical($render, "0", 'By default, "0" should not be treated as empty.');
73
74 // Test when results are not rewritten and non-zero empty values are hidden.
75 $view->field['name']->options['hide_alter_empty'] = TRUE;
76 $view->field['name']->options['hide_empty'] = TRUE;
77 $view->field['name']->options['empty_zero'] = FALSE;
78
79 // Test a valid string.
80 $view->result[0]->{$column_map_reversed['name']} = $random_name;
81 $render = $view->field['name']->advanced_render($view->result[0]);
82 $this->assertIdentical($render, $random_name, 'If hide_empty is checked, a string should not be treated as empty.');
83
84 // Test an empty string.
85 $view->result[0]->{$column_map_reversed['name']} = "";
86 $render = $view->field['name']->advanced_render($view->result[0]);
87 $this->assertIdentical($render, "", 'If hide_empty is checked, "" should be treated as empty.');
88
89 // Test zero as an integer.
90 $view->result[0]->{$column_map_reversed['name']} = 0;
91 $render = $view->field['name']->advanced_render($view->result[0]);
92 $this->assertIdentical($render, '0', 'If hide_empty is checked, but not empty_zero, 0 should not be treated as empty.');
93
94 // Test zero as a string.
95 $view->result[0]->{$column_map_reversed['name']} = "0";
96 $render = $view->field['name']->advanced_render($view->result[0]);
97 $this->assertIdentical($render, "0", 'If hide_empty is checked, but not empty_zero, "0" should not be treated as empty.');
98
99 // Test when results are not rewritten and all empty values are hidden.
100 $view->field['name']->options['hide_alter_empty'] = TRUE;
101 $view->field['name']->options['hide_empty'] = TRUE;
102 $view->field['name']->options['empty_zero'] = TRUE;
103
104 // Test zero as an integer.
105 $view->result[0]->{$column_map_reversed['name']} = 0;
106 $render = $view->field['name']->advanced_render($view->result[0]);
107 $this->assertIdentical($render, "", 'If hide_empty and empty_zero are checked, 0 should be treated as empty.');
108
109 // Test zero as a string.
110 $view->result[0]->{$column_map_reversed['name']} = "0";
111 $render = $view->field['name']->advanced_render($view->result[0]);
112 $this->assertIdentical($render, "", 'If hide_empty and empty_zero are checked, "0" should be treated as empty.');
113
114 // Test when results are rewritten to a valid string and non-zero empty
115 // results are hidden.
116 $view->field['name']->options['hide_alter_empty'] = FALSE;
117 $view->field['name']->options['hide_empty'] = TRUE;
118 $view->field['name']->options['empty_zero'] = FALSE;
119 $view->field['name']->options['alter']['alter_text'] = TRUE;
120 $view->field['name']->options['alter']['text'] = $random_name;
121
122 // Test a valid string.
123 $view->result[0]->{$column_map_reversed['name']} = $random_value;
124 $render = $view->field['name']->advanced_render($view->result[0]);
125 $this->assertIdentical($render, $random_name, 'If the rewritten string is not empty, it should not be treated as empty.');
126
127 // Test an empty string.
128 $view->result[0]->{$column_map_reversed['name']} = "";
129 $render = $view->field['name']->advanced_render($view->result[0]);
130 $this->assertIdentical($render, $random_name, 'If the rewritten string is not empty, "" should not be treated as empty.');
131
132 // Test zero as an integer.
133 $view->result[0]->{$column_map_reversed['name']} = 0;
134 $render = $view->field['name']->advanced_render($view->result[0]);
135 $this->assertIdentical($render, $random_name, 'If the rewritten string is not empty, 0 should not be treated as empty.');
136
137 // Test zero as a string.
138 $view->result[0]->{$column_map_reversed['name']} = "0";
139 $render = $view->field['name']->advanced_render($view->result[0]);
140 $this->assertIdentical($render, $random_name, 'If the rewritten string is not empty, "0" should not be treated as empty.');
141
142 // Test when results are rewritten to an empty string and non-zero empty results are hidden.
143 $view->field['name']->options['hide_alter_empty'] = TRUE;
144 $view->field['name']->options['hide_empty'] = TRUE;
145 $view->field['name']->options['empty_zero'] = FALSE;
146 $view->field['name']->options['alter']['alter_text'] = TRUE;
147 $view->field['name']->options['alter']['text'] = "";
148
149 // Test a valid string.
150 $view->result[0]->{$column_map_reversed['name']} = $random_name;
151 $render = $view->field['name']->advanced_render($view->result[0]);
152 $this->assertIdentical($render, $random_name, 'If the rewritten string is empty, it should not be treated as empty.');
153
154 // Test an empty string.
155 $view->result[0]->{$column_map_reversed['name']} = "";
156 $render = $view->field['name']->advanced_render($view->result[0]);
157 $this->assertIdentical($render, "", 'If the rewritten string is empty, "" should be treated as empty.');
158
159 // Test zero as an integer.
160 $view->result[0]->{$column_map_reversed['name']} = 0;
161 $render = $view->field['name']->advanced_render($view->result[0]);
162 $this->assertIdentical($render, '0', 'If the rewritten string is empty, 0 should not be treated as empty.');
163
164 // Test zero as a string.
165 $view->result[0]->{$column_map_reversed['name']} = "0";
166 $render = $view->field['name']->advanced_render($view->result[0]);
167 $this->assertIdentical($render, "0", 'If the rewritten string is empty, "0" should not be treated as empty.');
168
169 // Test when results are rewritten to zero as a string and non-zero empty
170 // results are hidden.
171 $view->field['name']->options['hide_alter_empty'] = FALSE;
172 $view->field['name']->options['hide_empty'] = TRUE;
173 $view->field['name']->options['empty_zero'] = FALSE;
174 $view->field['name']->options['alter']['alter_text'] = TRUE;
175 $view->field['name']->options['alter']['text'] = "0";
176
177 // Test a valid string.
178 $view->result[0]->{$column_map_reversed['name']} = $random_name;
179 $render = $view->field['name']->advanced_render($view->result[0]);
180 $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.');
181
182 // Test an empty string.
183 $view->result[0]->{$column_map_reversed['name']} = "";
184 $render = $view->field['name']->advanced_render($view->result[0]);
185 $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.');
186
187 // Test zero as an integer.
188 $view->result[0]->{$column_map_reversed['name']} = 0;
189 $render = $view->field['name']->advanced_render($view->result[0]);
190 $this->assertIdentical($render, "0", 'If the rewritten string is zero and empty_zero is not checked, 0 should not be treated as empty.');
191
192 // Test zero as a string.
193 $view->result[0]->{$column_map_reversed['name']} = "0";
194 $render = $view->field['name']->advanced_render($view->result[0]);
195 $this->assertIdentical($render, "0", 'If the rewritten string is zero and empty_zero is not checked, "0" should not be treated as empty.');
196
197 // Test when results are rewritten to a valid string and non-zero empty
198 // results are hidden.
199 $view->field['name']->options['hide_alter_empty'] = TRUE;
200 $view->field['name']->options['hide_empty'] = TRUE;
201 $view->field['name']->options['empty_zero'] = FALSE;
202 $view->field['name']->options['alter']['alter_text'] = TRUE;
203 $view->field['name']->options['alter']['text'] = $random_value;
204
205 // Test a valid string.
206 $view->result[0]->{$column_map_reversed['name']} = $random_name;
207 $render = $view->field['name']->advanced_render($view->result[0]);
208 $this->assertIdentical($render, $random_value, 'If the original and rewritten strings are valid, it should not be treated as empty.');
209
210 // Test an empty string.
211 $view->result[0]->{$column_map_reversed['name']} = "";
212 $render = $view->field['name']->advanced_render($view->result[0]);
213 $this->assertIdentical($render, "", 'If either the original or rewritten string is invalid, "" should be treated as empty.');
214
215 // Test zero as an integer.
216 $view->result[0]->{$column_map_reversed['name']} = 0;
217 $render = $view->field['name']->advanced_render($view->result[0]);
218 $this->assertIdentical($render, $random_value, 'If the original and rewritten strings are valid, 0 should not be treated as empty.');
219
220 // Test zero as a string.
221 $view->result[0]->{$column_map_reversed['name']} = "0";
222 $render = $view->field['name']->advanced_render($view->result[0]);
223 $this->assertIdentical($render, $random_value, 'If the original and rewritten strings are valid, "0" should not be treated as empty.');
224
225 // Test when results are rewritten to zero as a string and all empty
226 // original values and results are hidden.
227 $view->field['name']->options['hide_alter_empty'] = TRUE;
228 $view->field['name']->options['hide_empty'] = TRUE;
229 $view->field['name']->options['empty_zero'] = TRUE;
230 $view->field['name']->options['alter']['alter_text'] = TRUE;
231 $view->field['name']->options['alter']['text'] = "0";
232
233 // Test a valid string.
234 $view->result[0]->{$column_map_reversed['name']} = $random_name;
235 $render = $view->field['name']->advanced_render($view->result[0]);
236 $this->assertIdentical($render, "", 'If the rewritten string is zero, it should be treated as empty.');
237
238 // Test an empty string.
239 $view->result[0]->{$column_map_reversed['name']} = "";
240 $render = $view->field['name']->advanced_render($view->result[0]);
241 $this->assertIdentical($render, "", 'If the rewritten string is zero, "" should be treated as empty.');
242
243 // Test zero as an integer.
244 $view->result[0]->{$column_map_reversed['name']} = 0;
245 $render = $view->field['name']->advanced_render($view->result[0]);
246 $this->assertIdentical($render, "", 'If the rewritten string is zero, 0 should not be treated as empty.');
247
248 // Test zero as a string.
249 $view->result[0]->{$column_map_reversed['name']} = "0";
250 $render = $view->field['name']->advanced_render($view->result[0]);
251 $this->assertIdentical($render, "", 'If the rewritten string is zero, "0" should not be treated as empty.');
252 }
253
254 /**
255 * Tests the usage of the empty text.
256 */
257 function _testEmptyText() {
258 $view = $this->getBasicView();
259 $view->init_display();
260 $this->executeView($view);
261
262 $column_map_reversed = array_flip($this->column_map);
263 $view->row_index = 0;
264
265 $empty_text = $view->field['name']->options['empty'] = $this->randomName();
266 $view->result[0]->{$column_map_reversed['name']} = "";
267 $render = $view->field['name']->advanced_render($view->result[0]);
268 $this->assertIdentical($render, $empty_text, 'If a field is empty, the empty text should be used for the output.');
269
270 $view->result[0]->{$column_map_reversed['name']} = "0";
271 $render = $view->field['name']->advanced_render($view->result[0]);
272 $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.');
273
274 $view->result[0]->{$column_map_reversed['name']} = "0";
275 $view->field['name']->options['empty_zero'] = TRUE;
276 $render = $view->field['name']->advanced_render($view->result[0]);
277 $this->assertIdentical($render, $empty_text, 'If a field is 0 and empty_zero is checked, the empty text should be used for the output.');
278
279 $view->result[0]->{$column_map_reversed['name']} = "";
280 $view->field['name']->options['alter']['alter_text'] = TRUE;
281 $alter_text = $view->field['name']->options['alter']['text'] = $this->randomName();
282 $view->field['name']->options['hide_alter_empty'] = FALSE;
283 $render = $view->field['name']->advanced_render($view->result[0]);
284 $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.');
285
286 $view->field['name']->options['hide_alter_empty'] = TRUE;
287 $render = $view->field['name']->advanced_render($view->result[0]);
288 $this->assertIdentical($render, $empty_text, 'If a field is empty, some rewrite text exists, and hide_alter_empty is checked, use the empty text.');
289 }
290
291 /**
292 * Tests views_handler_field::is_value_empty().
293 */
294 function testIsValueEmpty() {
295 $view = $this->getBasicView();
296 $view->init_display();
297 $view->init_handlers();
298 $field = $view->field['name'];
299
300 $this->assertFalse($field->is_value_empty("not empty", TRUE), 'A normal string is not empty.');
301 $this->assertTrue($field->is_value_empty("not empty", TRUE, FALSE), 'A normal string which skips empty() can be seen as empty.');
302
303 $this->assertTrue($field->is_value_empty("", TRUE), '"" is considered as empty.');
304
305 $this->assertTrue($field->is_value_empty('0', TRUE), '"0" is considered as empty if empty_zero is TRUE.');
306 $this->assertTrue($field->is_value_empty(0, TRUE), '0 is considered as empty if empty_zero is TRUE.');
307 $this->assertFalse($field->is_value_empty('0', FALSE), '"0" is considered not as empty if empty_zero is FALSE.');
308 $this->assertFalse($field->is_value_empty(0, FALSE), '0 is considered not as empty if empty_zero is FALSE.');
309
310 $this->assertTrue($field->is_value_empty(NULL, TRUE, TRUE), 'Null should be always seen as empty, regardless of no_skip_empty.');
311 $this->assertTrue($field->is_value_empty(NULL, TRUE, FALSE), 'Null should be always seen as empty, regardless of no_skip_empty.');
312 }
313
314 }