/plugins/search/tests/cases/components/prg.test.php

https://github.com/manubamba/cakephp-search-plugin-v1.1 · PHP · 373 lines · 210 code · 36 blank · 127 comment · 0 complexity · 230abd831b9cd145ffdde3f2b059c21f MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright 2009-2010, Cake Development Corporation (http://cakedc.com)
  4. *
  5. * Licensed under The MIT License
  6. * Redistributions of files must retain the above copyright notice.
  7. *
  8. * @copyright Copyright 2009-2010, Cake Development Corporation (http://cakedc.com)
  9. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  10. */
  11. App::import('Controller', 'Controller', false);
  12. App::import('Component', 'Search.Prg');
  13. /**
  14. * Post-Redirect-Get: Transfers POST Requests to GET Requests tests
  15. *
  16. * @package search
  17. * @subpackage search.tests.cases.components
  18. */
  19. class Post extends CakeTestModel {
  20. /**
  21. * Name
  22. *
  23. * @var string
  24. */
  25. public $name = 'Post';
  26. /**
  27. * Behaviors
  28. *
  29. * @var array
  30. */
  31. public $actsAs = array('Search.Searchable');
  32. }
  33. /**
  34. * PostsTest Controller
  35. *
  36. * @package search
  37. * @subpackage search.tests.cases.components
  38. */
  39. class PostsTestController extends Controller {
  40. /**
  41. * Controller name
  42. *
  43. * @var string
  44. */
  45. public $name = 'PostsTest';
  46. /**
  47. * Models to use
  48. *
  49. * @var array
  50. */
  51. public $uses = array('Post');
  52. /**
  53. * Components
  54. *
  55. * @var array
  56. */
  57. public $components = array('Search.Prg', 'Session');
  58. /**
  59. * beforeFilter
  60. *
  61. * @return void
  62. */
  63. public function beforeFilter() {
  64. parent::beforeFilter();
  65. $this->Prg->actions = array(
  66. 'search' => array(
  67. 'controller' => 'Posts',
  68. 'action' => 'result'));
  69. }
  70. /**
  71. * Overwrite redirect
  72. *
  73. * @param string $url
  74. * @param string $status
  75. * @param string $exit
  76. * @return void
  77. */
  78. public function redirect($url, $status = NULL, $exit = true) {
  79. $this->redirectUrl = $url;
  80. }
  81. }
  82. /**
  83. * PRG Component Test
  84. *
  85. * @package search
  86. * @subpackage search.tests.cases.components
  87. */
  88. class PrgComponentTest extends CakeTestCase {
  89. /**
  90. * Fixtures
  91. *
  92. * @var array
  93. */
  94. public $fixtures = array('plugin.search.Post');
  95. /**
  96. * startTest
  97. *
  98. * @return void
  99. */
  100. function startTest() {
  101. $this->Controller = new PostsTestController();
  102. $this->Controller->constructClasses();
  103. $this->Controller->params = array(
  104. 'named' => array(),
  105. 'pass' => array(),
  106. 'url' => array());
  107. }
  108. /**
  109. * endTest
  110. *
  111. * @return void
  112. */
  113. function endTest() {
  114. unset($this->Controller);
  115. ClassRegistry::flush();
  116. }
  117. /**
  118. * test
  119. *
  120. * @return void
  121. */
  122. public function testPresetForm() {
  123. $this->Controller->presetVars = array(
  124. array(
  125. 'field' => 'title',
  126. 'type' => 'value'),
  127. array(
  128. 'field' => 'checkbox',
  129. 'type' => 'checkbox'),
  130. array(
  131. 'field' => 'lookup',
  132. 'type' => 'lookup',
  133. 'formField' => 'lookup_input',
  134. 'modelField' => 'title',
  135. 'model' => 'Post'));
  136. $this->Controller->passedArgs = array(
  137. 'title' => 'test',
  138. 'checkbox' => 'test|test2|test3',
  139. 'lookup' => '1');
  140. $this->Controller->Component->init($this->Controller);
  141. $this->Controller->Component->initialize($this->Controller);
  142. $this->Controller->beforeFilter();
  143. ClassRegistry::addObject('view', new View($this->Controller));
  144. $this->Controller->Prg->presetForm('Post');
  145. $expected = array(
  146. 'Post' => array(
  147. 'title' => 'test',
  148. 'checkbox' => array(
  149. 0 => 'test',
  150. 1 => 'test2',
  151. 2 => 'test3'),
  152. 'lookup' => 1,
  153. 'lookup_input' => 'First Post'));
  154. $this->assertEqual($this->Controller->data, $expected);
  155. }
  156. /**
  157. * This test checks that the search on an integer type field in the database
  158. * works correctly when a 0 (zero) is entered in the form.
  159. *
  160. * @return void
  161. * @link http://github.com/CakeDC/Search/issues#issue/3
  162. */
  163. public function testPresetFormWithIntegerField() {
  164. $this->Controller->presetVars = array(
  165. array(
  166. 'field' => 'views',
  167. 'type' => 'value'));
  168. $this->Controller->passedArgs = array(
  169. 'views' => '0');
  170. $this->Controller->Component->init($this->Controller);
  171. $this->Controller->Component->initialize($this->Controller);
  172. $this->Controller->beforeFilter();
  173. ClassRegistry::addObject('view', new View($this->Controller));
  174. $this->Controller->Prg->presetForm('Post');
  175. $expected = array(
  176. 'Post' => array(
  177. 'views' => '0'));
  178. $this->assertEqual($this->Controller->data, $expected);
  179. }
  180. /**
  181. * testFixFormValues
  182. *
  183. * @return void
  184. */
  185. public function testSerializeParams() {
  186. $this->Controller->presetVars = array(
  187. array(
  188. 'field' => 'options',
  189. 'type' => 'checkbox'));
  190. $this->Controller->Component->init($this->Controller);
  191. $this->Controller->Component->initialize($this->Controller);
  192. $testData = array(
  193. 'options' => array(
  194. 0 => 'test1', 1 => 'test2', 2 => 'test3'));
  195. $result = $this->Controller->Prg->serializeParams($testData);
  196. $this->assertEqual($result, array('options' => 'test1|test2|test3'));
  197. $testData = array('options' => '');
  198. $result = $this->Controller->Prg->serializeParams($testData);
  199. $this->assertEqual($result, array('options' => ''));
  200. }
  201. /**
  202. * testConnectNamed
  203. *
  204. * @return void
  205. */
  206. public function testConnectNamed() {
  207. $this->Controller->passedArgs = array(
  208. 'title' => 'test');
  209. $this->Controller->Component->init($this->Controller);
  210. $this->Controller->Component->initialize($this->Controller);
  211. $this->assertFalse($this->Controller->Prg->connectNamed());
  212. $this->assertFalse($this->Controller->Prg->connectNamed(1));
  213. }
  214. /**
  215. * testExclude
  216. *
  217. * @return void
  218. */
  219. public function testExclude() {
  220. $this->Controller->params['named'] = array();
  221. $this->Controller->Component->init($this->Controller);
  222. $this->Controller->Component->initialize($this->Controller);
  223. $array = array('foo' => 'test', 'bar' => 'test', 'test' => 'test');
  224. $exclude = array('bar', 'test');
  225. $this->assertEqual($this->Controller->Prg->exclude($array, $exclude), array('foo' => 'test'));
  226. }
  227. /**
  228. * testCommonProcess
  229. *
  230. * @return void
  231. */
  232. public function testCommonProcess() {
  233. $this->Controller->params['named'] = array();
  234. $this->Controller->Component->init($this->Controller);
  235. $this->Controller->Component->initialize($this->Controller);
  236. $this->Controller->presetVars = array();
  237. $this->Controller->action = 'search';
  238. $this->Controller->data = array(
  239. 'Post' => array(
  240. 'title' => 'test'));
  241. $this->Controller->Prg->commonProcess('Post', array(
  242. 'form' => 'Post',
  243. 'modelMethod' => false));
  244. $this->assertEqual($this->Controller->redirectUrl, array(
  245. 'title' => 'test',
  246. 'action' => 'search'));
  247. $this->Controller->Prg->commonProcess(null, array(
  248. 'modelMethod' => false));
  249. $this->assertEqual($this->Controller->redirectUrl, array(
  250. 'title' => 'test',
  251. 'action' => 'search'));
  252. $this->Controller->Post->filterArgs = array(
  253. array('name' => 'title', 'type' => 'value'));
  254. $this->Controller->Prg->commonProcess('Post');
  255. $this->assertEqual($this->Controller->redirectUrl, array(
  256. 'title' => 'test',
  257. 'action' => 'search'));
  258. }
  259. /**
  260. * testCommonProcessGet
  261. *
  262. * @return void
  263. */
  264. public function testCommonProcessGet() {
  265. $this->Controller->Component->init($this->Controller);
  266. $this->Controller->Component->initialize($this->Controller);
  267. $this->Controller->action = 'search';
  268. $this->Controller->presetVars = array(
  269. array('field' => 'title', 'type' => 'value'));
  270. $this->Controller->data = array();
  271. $this->Controller->Post->filterArgs = array(
  272. array('name' => 'title', 'type' => 'value'));
  273. $this->Controller->params['named'] = array('title' => 'test');
  274. $this->Controller->passedArgs = array_merge($this->Controller->params['named'], $this->Controller->params['pass']);
  275. $this->Controller->Prg->commonProcess('Post');
  276. $this->assertEqual($this->Controller->data, array('Post' => array('title' => 'test')));
  277. }
  278. /**
  279. * testSerializeParamsWithEncoding
  280. *
  281. * @return void
  282. */
  283. public function testSerializeParamsWithEncoding() {
  284. $this->Controller->Component->init($this->Controller);
  285. $this->Controller->Component->initialize($this->Controller);
  286. $this->Controller->action = 'search';
  287. $this->Controller->presetVars = array(
  288. array('field' => 'title', 'type' => 'value', 'encode' => true));
  289. $this->Controller->data = array();
  290. $this->Controller->Post->filterArgs = array(
  291. array('name' => 'title', 'type' => 'value'));
  292. $this->Controller->Prg->encode = true;
  293. $test = array('title' => 'Something new');
  294. $result = $this->Controller->Prg->serializeParams($test);
  295. $this->assertEqual($result['title'], base64_encode('Something new'));
  296. }
  297. /**
  298. * testPresetFormWithEncodedParams
  299. *
  300. * @return void
  301. */
  302. public function testPresetFormWithEncodedParams() {
  303. $this->Controller->presetVars = array(
  304. array(
  305. 'field' => 'title',
  306. 'type' => 'value'),
  307. array(
  308. 'field' => 'checkbox',
  309. 'type' => 'checkbox'),
  310. array(
  311. 'field' => 'lookup',
  312. 'type' => 'lookup',
  313. 'formField' => 'lookup_input',
  314. 'modelField' => 'title',
  315. 'model' => 'Post'));
  316. $this->Controller->passedArgs = array(
  317. 'title' => base64_encode('test'),
  318. 'checkbox' => base64_encode('test|test2|test3'),
  319. 'lookup' => base64_encode('1'));
  320. $this->Controller->Component->init($this->Controller);
  321. $this->Controller->Component->initialize($this->Controller);
  322. $this->Controller->beforeFilter();
  323. ClassRegistry::addObject('view', new View($this->Controller));
  324. $this->Controller->Prg->encode = true;
  325. $this->Controller->Prg->presetForm('Post');
  326. $expected = array(
  327. 'Post' => array(
  328. 'title' => 'test',
  329. 'checkbox' => array(
  330. 0 => 'test',
  331. 1 => 'test2',
  332. 2 => 'test3'),
  333. 'lookup' => 1,
  334. 'lookup_input' => 'First Post'));
  335. $this->assertEqual($this->Controller->data, $expected);
  336. }
  337. }