/tests/cases/components/ratings.test.php

https://github.com/siran/ratings · PHP · 306 lines · 146 code · 33 blank · 127 comment · 0 complexity · 9ae5380e385ec53c6fa609bcc26ecf46 MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright 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 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', array('Ratings.Ratings', 'Session', 'Auth'));
  13. Mock::generate('AuthComponent');
  14. Mock::generate('SessionComponent');
  15. /**
  16. * Test Article Model
  17. *
  18. * @package ratings
  19. * @subpackage ratings.tests.cases.components
  20. */
  21. class Article extends CakeTestModel {
  22. /**
  23. * Model name
  24. *
  25. * @var string
  26. */
  27. public $name = 'Article';
  28. }
  29. /**
  30. * Test ArticlesTestController
  31. *
  32. * @package ratings
  33. * @subpackage ratings.tests.cases.components
  34. */
  35. class ArticlesTestController extends Controller {
  36. /**
  37. * Controller Name
  38. *
  39. * @var string
  40. */
  41. public $name = 'ArticlesTest';
  42. /**
  43. * Models used
  44. *
  45. * @var array
  46. */
  47. public $uses = array('Article');
  48. /**
  49. * Components used
  50. *
  51. * @var array
  52. */
  53. public $components = array('Ratings.Ratings', 'Session', 'Auth');
  54. /**
  55. * test method
  56. *
  57. * @return void
  58. */
  59. public function test() {
  60. return;
  61. }
  62. /**
  63. * Overloaded redirect
  64. *
  65. * @param string $url
  66. * @param string $status
  67. * @param string $exit
  68. * @return void
  69. */
  70. public function redirect($url, $status = NULL, $exit = true) {
  71. $this->redirect = $url;
  72. }
  73. }
  74. /**
  75. * Test RatingsComponentTest
  76. *
  77. * @package ratings
  78. * @subpackage ratings.tests.cases.components
  79. */
  80. class RatingsComponentTest extends CakeTestCase {
  81. /**
  82. * Controller using the tested component
  83. *
  84. * @var ArticlesTestController
  85. */
  86. public $Controller;
  87. /**
  88. * Mock AuthComponent object
  89. *
  90. * @var MockAuthComponent
  91. */
  92. public $AuthComponent;
  93. /**
  94. * Mock SessionComponent object
  95. *
  96. * @var MockSessionComponent
  97. */
  98. public $SessionComponent;
  99. /**
  100. * Fixtures
  101. *
  102. * @var array
  103. */
  104. public $fixtures = array(
  105. 'plugin.ratings.rating',
  106. 'plugin.ratings.article',
  107. 'plugin.ratings.user');
  108. /**
  109. * startTest method
  110. *
  111. * @return void
  112. */
  113. function startTest() {
  114. $this->Controller = new ArticlesTestController();
  115. $this->Controller->modelClass = 'Article';
  116. $this->Controller->constructClasses();
  117. $this->AuthComponent = new MockAuthComponent();
  118. $this->AuthComponent->enabled = true;
  119. $this->Controller->Auth = $this->AuthComponent;
  120. $this->SessionComponent = new MockSessionComponent();
  121. $this->SessionComponent->enabled = true;
  122. $this->Controller->Session = $this->SessionComponent;
  123. }
  124. /**
  125. * endTest method
  126. *
  127. * @return void
  128. */
  129. function endTest() {
  130. $this->Controller->Session->destroy();
  131. unset($this->Controller);
  132. ClassRegistry::flush();
  133. }
  134. /**
  135. * testInitialize
  136. *
  137. * @return void
  138. */
  139. public function testInitialize() {
  140. $this->__initControllerAndRatings(array(), false);
  141. $this->assertEqual($this->Controller->helpers, array(
  142. 'Session', 'Html', 'Form', 'Ratings.Rating'));
  143. $this->assertTrue($this->Controller->Article->Behaviors->attached('Ratable'), 'Ratable behavior should attached.');
  144. $this->assertEqual($this->Controller->Ratings->modelName, 'Article');
  145. }
  146. /**
  147. * testInitializeWithParamsForBehavior
  148. *
  149. * @return void
  150. */
  151. public function testInitializeWithParamsForBehavior() {
  152. $this->Controller->components = array('Ratings.Ratings' => array('update' => true), 'Session', 'Auth');
  153. $this->__initControllerAndRatings(array(), false);
  154. $this->assertEqual($this->Controller->helpers, array(
  155. 'Session', 'Html', 'Form', 'Ratings.Rating'));
  156. $this->assertTrue($this->Controller->Article->Behaviors->attached('Ratable'), 'Ratable behavior should attached.');
  157. $this->assertTrue($this->Controller->Article->Behaviors->Ratable->settings['Article']['update'], 'Ratable behavior should be updatable.');
  158. $this->assertEqual($this->Controller->Ratings->modelName, 'Article');
  159. }
  160. /**
  161. * testInitializeWithParamsForComponent
  162. *
  163. * @return void
  164. */
  165. public function testInitializeWithParamsForComponent() {
  166. $this->Controller->components = array('Ratings.Ratings' => array('actionNames' => array('show')), 'Session', 'Auth');
  167. $this->__initControllerAndRatings(array(), false);
  168. $this->assertEqual($this->Controller->helpers, array(
  169. 'Session', 'Html', 'Form', 'Ratings.Rating'));
  170. $this->assertTrue($this->Controller->Article->Behaviors->attached('Ratable'), 'Ratable behavior should attached.');
  171. $this->assertEqual($this->Controller->Ratings->actionNames, array('show'));
  172. $this->assertEqual($this->Controller->Ratings->modelName, 'Article');
  173. }
  174. /**
  175. * testStartup
  176. *
  177. * @return void
  178. */
  179. public function testStartup() {
  180. $this->AuthComponent->setReturnValue('user', '1', array('id'));
  181. $params = array(
  182. 'plugin' => null,
  183. 'controller' => 'articles',
  184. 'action' => 'test',
  185. 'pass' => array(),
  186. 'named' => array(
  187. 'rating' => '5',
  188. 'rate' => '2',
  189. 'redirect' => true));
  190. $expectedRedirect = array(
  191. 'plugin' => null,
  192. 'controller' => 'articles',
  193. 'action' => 'test');
  194. $this->Controller->Session->expectCallCount('setFlash', 3);
  195. $this->Controller->Session->expectAt(0, 'setFlash', array('Your rate was successfull.', 'default', array(), 'success'));
  196. $this->Controller->Session->expectAt(1, 'setFlash', array('You have already rated.', 'default', array(), 'error'));
  197. $this->Controller->Session->expectAt(2, 'setFlash', array('Invalid rate.', 'default', array(), 'error'));
  198. $this->Controller->Session->write('Message', null);
  199. $this->__initControllerAndRatings($params);
  200. $this->assertEqual($this->Controller->redirect, $expectedRedirect);
  201. $this->Controller->Session->write('Message', null);
  202. $params['named']['rate'] = '1';
  203. $this->__initControllerAndRatings($params);
  204. $this->assertEqual($this->Controller->redirect, $expectedRedirect);
  205. $this->Controller->Session->write('Message', null);
  206. $params['named']['rate'] = 'invalid-record!';
  207. $this->__initControllerAndRatings($params);
  208. $this->assertEqual($this->Controller->redirect, $expectedRedirect);
  209. }
  210. /**
  211. * testStartupAcceptPost
  212. *
  213. * @return void
  214. */
  215. public function testStartupAcceptPost() {
  216. $this->AuthComponent->setReturnValue('user', '1', array('id'));
  217. $params = array(
  218. 'plugin' => null,
  219. 'controller' => 'articles',
  220. 'action' => 'test',
  221. 'pass' => array(),
  222. 'named' => array(
  223. 'rate' => '2',
  224. 'redirect' => true));
  225. $expectedRedirect = array(
  226. 'plugin' => null,
  227. 'controller' => 'articles',
  228. 'action' => 'test');
  229. $this->Controller->data = array('Article' => array('rating' => 2));
  230. $this->Controller->Session->write('Message', null);
  231. $this->Controller->Session->expectOnce('setFlash');
  232. $this->__initControllerAndRatings($params);
  233. $this->assertEqual($this->Controller->redirect, $expectedRedirect);
  234. }
  235. /**
  236. * testBuildUrl
  237. *
  238. * @return void
  239. */
  240. public function testBuildUrl() {
  241. $params = array(
  242. 'plugin' => null,
  243. 'controller' => 'articles',
  244. 'action' => 'test',
  245. 'pass' => array(),
  246. 'named' => array(
  247. 'foo' => 'bar',
  248. 'rating' => 'test',
  249. 'rate' => '5',
  250. 'redirect' => true));
  251. $this->__initControllerAndRatings($params);
  252. $result = $this->Controller->Ratings->buildUrl();
  253. $this->assertEqual($result, array(
  254. 'plugin' => null,
  255. 'controller' => 'articles',
  256. 'action' => 'test',
  257. 'foo' => 'bar'));
  258. }
  259. /**
  260. * Convenience method for testing: Initializes the controller and the Ratings component
  261. *
  262. * @param array $params Controller params
  263. * @param boolean $doStartup Whether or not startup has to be called on the Ratings Component
  264. * @return void
  265. */
  266. private function __initControllerAndRatings($params = array(), $doStartup = true) {
  267. $_default = array('named' => array(), 'pass' => array());
  268. $this->Controller->params = array_merge($_default, $params);
  269. $this->Controller->Component->init($this->Controller);
  270. $this->Controller->Component->initialize($this->Controller);
  271. $this->Controller->Ratings->startup($this->Controller);
  272. }
  273. }