views_argument_validator.test 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * @file
  4. * Definition of ViewsArgumentValidatorTest.
  5. */
  6. /**
  7. * Tests Views argument validators.
  8. */
  9. class ViewsArgumentValidatorTest extends ViewsSqlTest {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Argument validator',
  13. 'group' => 'Views Plugins',
  14. 'description' => 'Test argument validator tests.',
  15. );
  16. }
  17. function testArgumentValidatePhp() {
  18. $string = $this->randomName();
  19. $view = $this->view_test_argument_validate_php($string);
  20. $view->set_display('default');
  21. $view->pre_execute();
  22. $view->init_handlers();
  23. $this->assertTrue($view->argument['null']->validate_arg($string));
  24. // Reset safed argument validation.
  25. $view->argument['null']->argument_validated = NULL;
  26. $this->assertFalse($view->argument['null']->validate_arg($this->randomName()));
  27. }
  28. function testArgumentValidateNumeric() {
  29. $view = $this->view_argument_validate_numeric();
  30. $view->set_display('default');
  31. $view->pre_execute();
  32. $view->init_handlers();
  33. $this->assertFalse($view->argument['null']->validate_arg($this->randomString()));
  34. // Reset safed argument validation.
  35. $view->argument['null']->argument_validated = NULL;
  36. $this->assertTrue($view->argument['null']->validate_arg(12));
  37. }
  38. /**
  39. * Make sure argument validation works properly.
  40. */
  41. function testArgumentValidatePhpFailure() {
  42. $view = $this->view_test_argument_validate_php_failure();
  43. $view->save();
  44. $this->drupalGet('test-php-failure');
  45. // This should return a 403, indicating that the arguments validation fails.
  46. $this->assertResponse(403);
  47. $this->drupalGet('test-php-failure-feed');
  48. // This should return a 403, indicating that the arguments validation fails.
  49. $this->assertResponse(403);
  50. }
  51. function view_test_argument_validate_php($string) {
  52. $code = 'return $argument == \'' . $string . '\';';
  53. $view = new view();
  54. $view->name = 'view_argument_validate_php';
  55. $view->description = '';
  56. $view->tag = '';
  57. $view->view_php = '';
  58. $view->base_table = 'node';
  59. $view->is_cacheable = FALSE;
  60. $view->api_version = 2;
  61. $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
  62. /* Display: Master */
  63. $handler = $view->new_display('default', 'Master', 'default');
  64. $handler->display->display_options['access']['type'] = 'none';
  65. $handler->display->display_options['cache']['type'] = 'none';
  66. $handler->display->display_options['exposed_form']['type'] = 'basic';
  67. $handler->display->display_options['pager']['type'] = 'full';
  68. $handler->display->display_options['style_plugin'] = 'default';
  69. $handler->display->display_options['row_plugin'] = 'fields';
  70. /* Argument: Global: Null */
  71. $handler->display->display_options['arguments']['null']['id'] = 'null';
  72. $handler->display->display_options['arguments']['null']['table'] = 'views';
  73. $handler->display->display_options['arguments']['null']['field'] = 'null';
  74. $handler->display->display_options['arguments']['null']['style_plugin'] = 'default_summary';
  75. $handler->display->display_options['arguments']['null']['default_argument_type'] = 'fixed';
  76. $handler->display->display_options['arguments']['null']['validate_type'] = 'php';
  77. $handler->display->display_options['arguments']['null']['validate_options']['code'] = $code;
  78. $handler->display->display_options['arguments']['null']['must_not_be'] = 0;
  79. return $view;
  80. }
  81. function view_argument_validate_numeric() {
  82. $view = new view();
  83. $view->name = 'view_argument_validate_numeric';
  84. $view->description = '';
  85. $view->tag = '';
  86. $view->view_php = '';
  87. $view->base_table = 'node';
  88. $view->is_cacheable = FALSE;
  89. $view->api_version = 2;
  90. $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
  91. /* Display: Master */
  92. $handler = $view->new_display('default', 'Master', 'default');
  93. $handler->display->display_options['access']['type'] = 'none';
  94. $handler->display->display_options['cache']['type'] = 'none';
  95. $handler->display->display_options['exposed_form']['type'] = 'basic';
  96. $handler->display->display_options['pager']['type'] = 'full';
  97. $handler->display->display_options['style_plugin'] = 'default';
  98. $handler->display->display_options['row_plugin'] = 'fields';
  99. /* Argument: Global: Null */
  100. $handler->display->display_options['arguments']['null']['id'] = 'null';
  101. $handler->display->display_options['arguments']['null']['table'] = 'views';
  102. $handler->display->display_options['arguments']['null']['field'] = 'null';
  103. $handler->display->display_options['arguments']['null']['style_plugin'] = 'default_summary';
  104. $handler->display->display_options['arguments']['null']['default_argument_type'] = 'fixed';
  105. $handler->display->display_options['arguments']['null']['validate_type'] = 'numeric';
  106. $handler->display->display_options['arguments']['null']['must_not_be'] = 0;
  107. return $view;
  108. }
  109. /**
  110. *
  111. *
  112. * @return view $view
  113. * The required view object.
  114. */
  115. function view_test_argument_validate_php_failure() {
  116. $view = new view();
  117. $view->name = 'view_argument_validate_php_failure';
  118. $view->description = '';
  119. $view->tag = '';
  120. $view->base_table = 'node';
  121. $view->human_name = '';
  122. $view->core = 0;
  123. $view->api_version = '3.0';
  124. $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
  125. /* Display: Master */
  126. $handler = $view->new_display('default', 'Master', 'default');
  127. $handler->display->display_options['use_more_always'] = FALSE;
  128. $handler->display->display_options['access']['type'] = 'none';
  129. $handler->display->display_options['cache']['type'] = 'none';
  130. $handler->display->display_options['query']['type'] = 'views_query';
  131. $handler->display->display_options['exposed_form']['type'] = 'basic';
  132. $handler->display->display_options['pager']['type'] = 'none';
  133. $handler->display->display_options['style_plugin'] = 'default';
  134. $handler->display->display_options['row_plugin'] = 'fields';
  135. /* Field: Content: Title */
  136. $handler->display->display_options['fields']['title']['id'] = 'title';
  137. $handler->display->display_options['fields']['title']['table'] = 'node';
  138. $handler->display->display_options['fields']['title']['field'] = 'title';
  139. $handler->display->display_options['fields']['title']['label'] = '';
  140. $handler->display->display_options['fields']['title']['element_label_colon'] = FALSE;
  141. $handler->display->display_options['fields']['title']['link_to_node'] = FALSE;
  142. /* Contextual filter: Global: Null */
  143. $handler->display->display_options['arguments']['null']['id'] = 'null';
  144. $handler->display->display_options['arguments']['null']['table'] = 'views';
  145. $handler->display->display_options['arguments']['null']['field'] = 'null';
  146. $handler->display->display_options['arguments']['null']['default_action'] = 'default';
  147. $handler->display->display_options['arguments']['null']['default_argument_type'] = 'fixed';
  148. $handler->display->display_options['arguments']['null']['default_argument_options']['argument'] = 'No filter';
  149. $handler->display->display_options['arguments']['null']['summary']['number_of_records'] = '0';
  150. $handler->display->display_options['arguments']['null']['summary']['format'] = 'default_summary';
  151. $handler->display->display_options['arguments']['null']['summary_options']['items_per_page'] = '25';
  152. $handler->display->display_options['arguments']['null']['specify_validation'] = TRUE;
  153. $handler->display->display_options['arguments']['null']['validate']['type'] = 'php';
  154. $handler->display->display_options['arguments']['null']['validate_options']['code'] = 'return FALSE;';
  155. $handler->display->display_options['arguments']['null']['validate']['fail'] = 'access denied';
  156. /* Display: Page */
  157. $handler = $view->new_display('page', 'Page', 'page_1');
  158. $handler->display->display_options['path'] = 'test-php-failure';
  159. /* Display: Feed */
  160. $handler = $view->new_display('feed', 'Feed', 'feed_1');
  161. $handler->display->display_options['path'] = 'test-php-failure-feed';
  162. $handler->display->display_options['pager']['type'] = 'none';
  163. $handler->display->display_options['pager']['options']['offset'] = '0';
  164. $handler->display->display_options['style_plugin'] = 'rss';
  165. $handler->display->display_options['row_plugin'] = 'node_rss';
  166. return $view;
  167. }
  168. }