views_handler_field.test 15 KB

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