views_handlers.test 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * @file
  4. * Definition of ViewsHandlersTest.
  5. */
  6. /**
  7. * Tests abstract handlers of views.
  8. */
  9. class ViewsHandlersTest extends ViewsSqlTest {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Handlers test',
  13. 'description' => 'test abstract handler definitions',
  14. 'group' => 'Views',
  15. );
  16. }
  17. protected function setUp() {
  18. parent::setUp('views', 'views_ui');
  19. module_enable(array('views_ui'));
  20. }
  21. function testFilterInOperatorUi() {
  22. $admin_user = $this->drupalCreateUser(array('administer views', 'administer site configuration'));
  23. $this->drupalLogin($admin_user);
  24. menu_rebuild();
  25. $path = 'admin/structure/views/nojs/config-item/test_filter_in_operator_ui/default/filter/type';
  26. $this->drupalGet($path);
  27. $this->assertFieldByName('options[expose][reduce]', FALSE);
  28. $edit = array(
  29. 'options[expose][reduce]' => TRUE,
  30. );
  31. $this->drupalPost($path, $edit, t('Apply'));
  32. $this->drupalGet($path);
  33. $this->assertFieldByName('options[expose][reduce]', TRUE);
  34. }
  35. /**
  36. * Tests views_break_phrase_string function.
  37. */
  38. function test_views_break_phrase_string() {
  39. $empty_stdclass = new stdClass();
  40. $empty_stdclass->operator = 'or';
  41. $empty_stdclass->value = array();
  42. $null = NULL;
  43. // check defaults.
  44. $this->assertEqual($empty_stdclass, views_break_phrase_string('', $null));
  45. $handler = views_get_handler('node', 'title', 'argument');
  46. $this->assertEqual($handler, views_break_phrase_string('', $handler));
  47. // test ors.
  48. $handler = views_break_phrase_string('word1 word2+word');
  49. $this->assertEqualValue(array('word1', 'word2', 'word'), $handler);
  50. $this->assertEqual('or', $handler->operator);
  51. $handler = views_break_phrase_string('word1+word2+word');
  52. $this->assertEqualValue(array('word1', 'word2', 'word'), $handler);
  53. $this->assertEqual('or', $handler->operator);
  54. $handler = views_break_phrase_string('word1 word2 word');
  55. $this->assertEqualValue(array('word1', 'word2', 'word'), $handler);
  56. $this->assertEqual('or', $handler->operator);
  57. $handler = views_break_phrase_string('word-1+word-2+word');
  58. $this->assertEqualValue(array('word-1', 'word-2', 'word'), $handler);
  59. $this->assertEqual('or', $handler->operator);
  60. $handler = views_break_phrase_string('wõrd1+wõrd2+wõrd');
  61. $this->assertEqualValue(array('wõrd1', 'wõrd2', 'wõrd'), $handler);
  62. $this->assertEqual('or', $handler->operator);
  63. // test ands.
  64. $handler = views_break_phrase_string('word1,word2,word');
  65. $this->assertEqualValue(array('word1', 'word2', 'word'), $handler);
  66. $this->assertEqual('and', $handler->operator);
  67. $handler = views_break_phrase_string('word1 word2,word');
  68. $this->assertEqualValue(array('word1 word2', 'word'), $handler);
  69. $this->assertEqual('and', $handler->operator);
  70. $handler = views_break_phrase_string('word1,word2 word');
  71. $this->assertEqualValue(array('word1', 'word2 word'), $handler);
  72. $this->assertEqual('and', $handler->operator);
  73. $handler = views_break_phrase_string('word-1,word-2,word');
  74. $this->assertEqualValue(array('word-1', 'word-2', 'word'), $handler);
  75. $this->assertEqual('and', $handler->operator);
  76. $handler = views_break_phrase_string('wõrd1,wõrd2,wõrd');
  77. $this->assertEqualValue(array('wõrd1', 'wõrd2', 'wõrd'), $handler);
  78. $this->assertEqual('and', $handler->operator);
  79. // test a single word.
  80. $handler = views_break_phrase_string('word');
  81. $this->assertEqualValue(array('word'), $handler);
  82. $this->assertEqual('and', $handler->operator);
  83. }
  84. /**
  85. * Tests views_break_phrase function.
  86. */
  87. function test_views_break_phrase() {
  88. $empty_stdclass = new stdClass();
  89. $empty_stdclass->operator = 'or';
  90. $empty_stdclass->value = array();
  91. $null = NULL;
  92. // check defaults.
  93. $this->assertEqual($empty_stdclass, views_break_phrase('', $null));
  94. $handler = views_get_handler('node', 'title', 'argument');
  95. $this->assertEqual($handler, views_break_phrase('', $handler));
  96. // Generate three random numbers which can be used below;
  97. $n1 = rand(0, 100);
  98. $n2 = rand(0, 100);
  99. $n3 = rand(0, 100);
  100. // test ors.
  101. $this->assertEqualValue(array($n1, $n2, $n3), views_break_phrase("$n1 $n2+$n3", $handler));
  102. $this->assertEqual('or', $handler->operator);
  103. $this->assertEqualValue(array($n1, $n2, $n3), views_break_phrase("$n1+$n2+$n3", $handler));
  104. $this->assertEqual('or', $handler->operator);
  105. $this->assertEqualValue(array($n1, $n2, $n3), views_break_phrase("$n1 $n2 $n3", $handler));
  106. $this->assertEqual('or', $handler->operator);
  107. $this->assertEqualValue(array($n1, $n2, $n3), views_break_phrase("$n1 $n2++$n3", $handler));
  108. $this->assertEqual('or', $handler->operator);
  109. // test ands.
  110. $this->assertEqualValue(array($n1, $n2, $n3), views_break_phrase("$n1,$n2,$n3", $handler));
  111. $this->assertEqual('and', $handler->operator);
  112. $this->assertEqualValue(array($n1, $n2, $n3), views_break_phrase("$n1,,$n2,$n3", $handler));
  113. $this->assertEqual('and', $handler->operator);
  114. }
  115. /**
  116. * Check to see if two values are equal.
  117. *
  118. * @param string $first
  119. * The first value to check.
  120. * @param views_handler $handler
  121. * @param string $message
  122. * The message to display along with the assertion.
  123. * @param string $group
  124. * The type of assertion - examples are "Browser", "PHP".
  125. *
  126. * @return bool
  127. * TRUE if the assertion succeeded, FALSE otherwise.
  128. */
  129. protected function assertEqualValue($first, $handler, $message = '', $group = 'Other') {
  130. return $this->assert($first == $handler->value, $message ? $message : t('First value is equal to second value'), $group);
  131. }
  132. }