views_handler_field.test 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. /**
  3. * @file
  4. * Definition of ViewsHandlerFieldTest.
  5. */
  6. /**
  7. * Tests the generic field handler.
  8. *
  9. * @see views_handler_field
  10. */
  11. class ViewsHandlerFieldTest extends ViewsSqlTest {
  12. /**
  13. *
  14. */
  15. public static function getInfo() {
  16. return array(
  17. 'name' => 'Field',
  18. 'description' => 'Test the core views_handler_field handler.',
  19. 'group' => 'Views Handlers',
  20. );
  21. }
  22. /**
  23. *
  24. */
  25. protected function setUp() {
  26. parent::setUp();
  27. $this->column_map = array(
  28. 'views_test_name' => 'name',
  29. );
  30. }
  31. /**
  32. * Tests the hide if empty functionality.
  33. *
  34. * This tests alters the result to get easier and less coupled results.
  35. */
  36. public function testHideIfEmpty() {
  37. $view = $this->getBasicView();
  38. $view->init_display();
  39. $this->executeView($view);
  40. $column_map_reversed = array_flip($this->column_map);
  41. $view->row_index = 0;
  42. $random_name = $this->randomName();
  43. $random_value = $this->randomName();
  44. // Test when results are not rewritten and empty values are not hidden.
  45. $view->field['name']->options['hide_alter_empty'] = FALSE;
  46. $view->field['name']->options['hide_empty'] = FALSE;
  47. $view->field['name']->options['empty_zero'] = FALSE;
  48. // Test a valid string.
  49. $view->result[0]->{$column_map_reversed['name']} = $random_name;
  50. $render = $view->field['name']->advanced_render($view->result[0]);
  51. $this->assertIdentical($render, $random_name, 'By default, a string should not be treated as empty.');
  52. // Test an empty string.
  53. $view->result[0]->{$column_map_reversed['name']} = "";
  54. $render = $view->field['name']->advanced_render($view->result[0]);
  55. $this->assertIdentical($render, "", 'By default, "" should not be treated as empty.');
  56. // Test zero as an integer.
  57. $view->result[0]->{$column_map_reversed['name']} = 0;
  58. $render = $view->field['name']->advanced_render($view->result[0]);
  59. $this->assertIdentical($render, '0', 'By default, 0 should not be treated as empty.');
  60. // Test zero as a string.
  61. $view->result[0]->{$column_map_reversed['name']} = "0";
  62. $render = $view->field['name']->advanced_render($view->result[0]);
  63. $this->assertIdentical($render, "0", 'By default, "0" should not be treated as empty.');
  64. // Test when results are not rewritten and non-zero empty values are hidden.
  65. $view->field['name']->options['hide_alter_empty'] = TRUE;
  66. $view->field['name']->options['hide_empty'] = TRUE;
  67. $view->field['name']->options['empty_zero'] = FALSE;
  68. // Test a valid string.
  69. $view->result[0]->{$column_map_reversed['name']} = $random_name;
  70. $render = $view->field['name']->advanced_render($view->result[0]);
  71. $this->assertIdentical($render, $random_name, 'If hide_empty is checked, a string should not be treated as empty.');
  72. // Test an empty string.
  73. $view->result[0]->{$column_map_reversed['name']} = "";
  74. $render = $view->field['name']->advanced_render($view->result[0]);
  75. $this->assertIdentical($render, "", 'If hide_empty is checked, "" should be treated as empty.');
  76. // Test zero as an integer.
  77. $view->result[0]->{$column_map_reversed['name']} = 0;
  78. $render = $view->field['name']->advanced_render($view->result[0]);
  79. $this->assertIdentical($render, '0', 'If hide_empty is checked, but not empty_zero, 0 should not be treated as empty.');
  80. // Test zero as a string.
  81. $view->result[0]->{$column_map_reversed['name']} = "0";
  82. $render = $view->field['name']->advanced_render($view->result[0]);
  83. $this->assertIdentical($render, "0", 'If hide_empty is checked, but not empty_zero, "0" should not be treated as empty.');
  84. // Test when results are not rewritten and all empty values are hidden.
  85. $view->field['name']->options['hide_alter_empty'] = TRUE;
  86. $view->field['name']->options['hide_empty'] = TRUE;
  87. $view->field['name']->options['empty_zero'] = TRUE;
  88. // Test zero as an integer.
  89. $view->result[0]->{$column_map_reversed['name']} = 0;
  90. $render = $view->field['name']->advanced_render($view->result[0]);
  91. $this->assertIdentical($render, "", 'If hide_empty and empty_zero are checked, 0 should be treated as empty.');
  92. // Test zero as a string.
  93. $view->result[0]->{$column_map_reversed['name']} = "0";
  94. $render = $view->field['name']->advanced_render($view->result[0]);
  95. $this->assertIdentical($render, "", 'If hide_empty and empty_zero are checked, "0" should be treated as empty.');
  96. // Test when results are rewritten to a valid string and non-zero empty
  97. // results are hidden.
  98. $view->field['name']->options['hide_alter_empty'] = FALSE;
  99. $view->field['name']->options['hide_empty'] = TRUE;
  100. $view->field['name']->options['empty_zero'] = FALSE;
  101. $view->field['name']->options['alter']['alter_text'] = TRUE;
  102. $view->field['name']->options['alter']['text'] = $random_name;
  103. // Test a valid string.
  104. $view->result[0]->{$column_map_reversed['name']} = $random_value;
  105. $render = $view->field['name']->advanced_render($view->result[0]);
  106. $this->assertIdentical($render, $random_name, 'If the rewritten string is not empty, it should not be treated as empty.');
  107. // Test an empty string.
  108. $view->result[0]->{$column_map_reversed['name']} = "";
  109. $render = $view->field['name']->advanced_render($view->result[0]);
  110. $this->assertIdentical($render, $random_name, 'If the rewritten string is not empty, "" should not be treated as empty.');
  111. // Test zero as an integer.
  112. $view->result[0]->{$column_map_reversed['name']} = 0;
  113. $render = $view->field['name']->advanced_render($view->result[0]);
  114. $this->assertIdentical($render, $random_name, 'If the rewritten string is not empty, 0 should not be treated as empty.');
  115. // Test zero as a string.
  116. $view->result[0]->{$column_map_reversed['name']} = "0";
  117. $render = $view->field['name']->advanced_render($view->result[0]);
  118. $this->assertIdentical($render, $random_name, 'If the rewritten string is not empty, "0" should not be treated as empty.');
  119. // Test when results are rewritten to an empty string and non-zero empty
  120. // results are hidden.
  121. $view->field['name']->options['hide_alter_empty'] = TRUE;
  122. $view->field['name']->options['hide_empty'] = TRUE;
  123. $view->field['name']->options['empty_zero'] = FALSE;
  124. $view->field['name']->options['alter']['alter_text'] = TRUE;
  125. $view->field['name']->options['alter']['text'] = "";
  126. // Test a valid string.
  127. $view->result[0]->{$column_map_reversed['name']} = $random_name;
  128. $render = $view->field['name']->advanced_render($view->result[0]);
  129. $this->assertIdentical($render, $random_name, 'If the rewritten string is empty, it should not be treated as empty.');
  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, "", 'If the rewritten string is empty, "" should be treated as empty.');
  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, '0', 'If the rewritten string is empty, 0 should not be treated as empty.');
  138. // Test zero as a string.
  139. $view->result[0]->{$column_map_reversed['name']} = "0";
  140. $render = $view->field['name']->advanced_render($view->result[0]);
  141. $this->assertIdentical($render, "0", 'If the rewritten string is empty, "0" should not be treated as empty.');
  142. // Test when results are rewritten to zero as a string and non-zero empty
  143. // results are hidden.
  144. $view->field['name']->options['hide_alter_empty'] = FALSE;
  145. $view->field['name']->options['hide_empty'] = TRUE;
  146. $view->field['name']->options['empty_zero'] = FALSE;
  147. $view->field['name']->options['alter']['alter_text'] = TRUE;
  148. $view->field['name']->options['alter']['text'] = "0";
  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, "0", 'If the rewritten string is zero and empty_zero is not checked, the string rewritten as 0 should not be treated as empty.');
  153. // Test an empty string.
  154. $view->result[0]->{$column_map_reversed['name']} = "";
  155. $render = $view->field['name']->advanced_render($view->result[0]);
  156. $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.');
  157. // Test zero as an integer.
  158. $view->result[0]->{$column_map_reversed['name']} = 0;
  159. $render = $view->field['name']->advanced_render($view->result[0]);
  160. $this->assertIdentical($render, "0", 'If the rewritten string is zero and empty_zero is not checked, 0 should not be treated as empty.');
  161. // Test zero as a string.
  162. $view->result[0]->{$column_map_reversed['name']} = "0";
  163. $render = $view->field['name']->advanced_render($view->result[0]);
  164. $this->assertIdentical($render, "0", 'If the rewritten string is zero and empty_zero is not checked, "0" should not be treated as empty.');
  165. // Test when results are rewritten to a valid string and non-zero empty
  166. // results are hidden.
  167. $view->field['name']->options['hide_alter_empty'] = TRUE;
  168. $view->field['name']->options['hide_empty'] = TRUE;
  169. $view->field['name']->options['empty_zero'] = FALSE;
  170. $view->field['name']->options['alter']['alter_text'] = TRUE;
  171. $view->field['name']->options['alter']['text'] = $random_value;
  172. // Test a valid string.
  173. $view->result[0]->{$column_map_reversed['name']} = $random_name;
  174. $render = $view->field['name']->advanced_render($view->result[0]);
  175. $this->assertIdentical($render, $random_value, 'If the original and rewritten strings are valid, it should not be treated as empty.');
  176. // Test an empty string.
  177. $view->result[0]->{$column_map_reversed['name']} = "";
  178. $render = $view->field['name']->advanced_render($view->result[0]);
  179. $this->assertIdentical($render, "", 'If either the original or rewritten string is invalid, "" should be treated as empty.');
  180. // Test zero as an integer.
  181. $view->result[0]->{$column_map_reversed['name']} = 0;
  182. $render = $view->field['name']->advanced_render($view->result[0]);
  183. $this->assertIdentical($render, $random_value, 'If the original and rewritten strings are valid, 0 should not be treated as empty.');
  184. // Test zero as a string.
  185. $view->result[0]->{$column_map_reversed['name']} = "0";
  186. $render = $view->field['name']->advanced_render($view->result[0]);
  187. $this->assertIdentical($render, $random_value, 'If the original and rewritten strings are valid, "0" should not be treated as empty.');
  188. // Test when results are rewritten to zero as a string and all empty
  189. // original values and results are hidden.
  190. $view->field['name']->options['hide_alter_empty'] = TRUE;
  191. $view->field['name']->options['hide_empty'] = TRUE;
  192. $view->field['name']->options['empty_zero'] = TRUE;
  193. $view->field['name']->options['alter']['alter_text'] = TRUE;
  194. $view->field['name']->options['alter']['text'] = "0";
  195. // Test a valid string.
  196. $view->result[0]->{$column_map_reversed['name']} = $random_name;
  197. $render = $view->field['name']->advanced_render($view->result[0]);
  198. $this->assertIdentical($render, "", 'If the rewritten string is zero, it should be treated as empty.');
  199. // Test an empty string.
  200. $view->result[0]->{$column_map_reversed['name']} = "";
  201. $render = $view->field['name']->advanced_render($view->result[0]);
  202. $this->assertIdentical($render, "", 'If the rewritten string is zero, "" should be treated as empty.');
  203. // Test zero as an integer.
  204. $view->result[0]->{$column_map_reversed['name']} = 0;
  205. $render = $view->field['name']->advanced_render($view->result[0]);
  206. $this->assertIdentical($render, "", 'If the rewritten string is zero, 0 should not be treated as empty.');
  207. // Test zero as a string.
  208. $view->result[0]->{$column_map_reversed['name']} = "0";
  209. $render = $view->field['name']->advanced_render($view->result[0]);
  210. $this->assertIdentical($render, "", 'If the rewritten string is zero, "0" should not be treated as empty.');
  211. }
  212. /**
  213. * Tests the usage of the empty text.
  214. */
  215. public function testEmptyText() {
  216. $view = $this->getBasicView();
  217. $view->init_display();
  218. $this->executeView($view);
  219. $column_map_reversed = array_flip($this->column_map);
  220. $view->row_index = 0;
  221. $empty_text = $view->field['name']->options['empty'] = $this->randomName();
  222. $view->result[0]->{$column_map_reversed['name']} = "";
  223. $render = $view->field['name']->advanced_render($view->result[0]);
  224. $this->assertIdentical($render, $empty_text, 'If a field is empty, the empty text should be used for the output.');
  225. $view->result[0]->{$column_map_reversed['name']} = "0";
  226. $render = $view->field['name']->advanced_render($view->result[0]);
  227. $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.');
  228. $view->result[0]->{$column_map_reversed['name']} = "0";
  229. $view->field['name']->options['empty_zero'] = TRUE;
  230. $render = $view->field['name']->advanced_render($view->result[0]);
  231. $this->assertIdentical($render, $empty_text, 'If a field is 0 and empty_zero is checked, the empty text should be used for the output.');
  232. $view->result[0]->{$column_map_reversed['name']} = "";
  233. $view->field['name']->options['alter']['alter_text'] = TRUE;
  234. $alter_text = $view->field['name']->options['alter']['text'] = $this->randomName();
  235. $view->field['name']->options['hide_alter_empty'] = FALSE;
  236. $render = $view->field['name']->advanced_render($view->result[0]);
  237. $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.');
  238. $view->field['name']->options['hide_alter_empty'] = TRUE;
  239. $render = $view->field['name']->advanced_render($view->result[0]);
  240. $this->assertIdentical($render, $empty_text, 'If a field is empty, some rewrite text exists, and hide_alter_empty is checked, use the empty text.');
  241. }
  242. /**
  243. * Tests views_handler_field::is_value_empty().
  244. */
  245. public function testIsValueEmpty() {
  246. $view = $this->getBasicView();
  247. $view->init_display();
  248. $view->init_handlers();
  249. $field = $view->field['name'];
  250. $this->assertFalse($field->is_value_empty("not empty", TRUE), 'A normal string is not empty.');
  251. $this->assertTrue($field->is_value_empty("not empty", TRUE, FALSE), 'A normal string which skips empty() can be seen as empty.');
  252. $this->assertTrue($field->is_value_empty("", TRUE), '"" is considered as empty.');
  253. $this->assertTrue($field->is_value_empty('0', TRUE), '"0" is considered as empty if empty_zero is TRUE.');
  254. $this->assertTrue($field->is_value_empty(0, TRUE), '0 is considered as empty if empty_zero is TRUE.');
  255. $this->assertFalse($field->is_value_empty('0', FALSE), '"0" is considered not as empty if empty_zero is FALSE.');
  256. $this->assertFalse($field->is_value_empty(0, FALSE), '0 is considered not as empty if empty_zero is FALSE.');
  257. $this->assertTrue($field->is_value_empty(NULL, TRUE, TRUE), 'Null should be always seen as empty, regardless of no_skip_empty.');
  258. $this->assertTrue($field->is_value_empty(NULL, TRUE, FALSE), 'Null should be always seen as empty, regardless of no_skip_empty.');
  259. }
  260. }