ObjectTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. <?php
  2. /**
  3. * ObjectTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  16. * @package Cake.Test.Case.Core
  17. * @since CakePHP(tm) v 1.2.0.5432
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. App::uses('Object', 'Core');
  21. App::uses('Router', 'Routing');
  22. App::uses('Controller', 'Controller');
  23. App::uses('Model', 'Model');
  24. /**
  25. * RequestActionPost class
  26. *
  27. * @package Cake.Test.Case.Core
  28. */
  29. class RequestActionPost extends CakeTestModel {
  30. /**
  31. * name property
  32. *
  33. * @var string 'ControllerPost'
  34. */
  35. public $name = 'RequestActionPost';
  36. /**
  37. * useTable property
  38. *
  39. * @var string 'posts'
  40. */
  41. public $useTable = 'posts';
  42. }
  43. /**
  44. * RequestActionController class
  45. *
  46. * @package Cake.Test.Case.Core
  47. */
  48. class RequestActionController extends Controller {
  49. /**
  50. * uses property
  51. *
  52. * @var array
  53. * @access public
  54. */
  55. public $uses = array('RequestActionPost');
  56. /**
  57. * test_request_action method
  58. *
  59. * @access public
  60. * @return void
  61. */
  62. public function test_request_action() {
  63. return 'This is a test';
  64. }
  65. /**
  66. * another_ra_test method
  67. *
  68. * @param mixed $id
  69. * @param mixed $other
  70. * @access public
  71. * @return void
  72. */
  73. public function another_ra_test($id, $other) {
  74. return $id + $other;
  75. }
  76. /**
  77. * normal_request_action method
  78. *
  79. * @return void
  80. */
  81. public function normal_request_action() {
  82. return 'Hello World';
  83. }
  84. /**
  85. * returns $this->here
  86. *
  87. * @return void
  88. */
  89. public function return_here() {
  90. return $this->here;
  91. }
  92. /**
  93. * paginate_request_action method
  94. *
  95. * @return void
  96. */
  97. public function paginate_request_action() {
  98. $this->paginate();
  99. return true;
  100. }
  101. /**
  102. * post pass, testing post passing
  103. *
  104. * @return array
  105. */
  106. public function post_pass() {
  107. return $this->request->data;
  108. }
  109. /**
  110. * test param passing and parsing.
  111. *
  112. * @return array
  113. */
  114. public function params_pass() {
  115. return $this->request;
  116. }
  117. public function param_check() {
  118. $this->autoRender = false;
  119. $content = '';
  120. if (isset($this->request->params[0])) {
  121. $content = 'return found';
  122. }
  123. $this->response->body($content);
  124. }
  125. }
  126. /**
  127. * TestObject class
  128. *
  129. * @package Cake.Test.Case.Core
  130. */
  131. class TestObject extends Object {
  132. /**
  133. * firstName property
  134. *
  135. * @var string 'Joel'
  136. */
  137. public $firstName = 'Joel';
  138. /**
  139. * lastName property
  140. *
  141. * @var string 'Moss'
  142. */
  143. public $lastName = 'Moss';
  144. /**
  145. * methodCalls property
  146. *
  147. * @var array
  148. */
  149. public $methodCalls = array();
  150. /**
  151. * emptyMethod method
  152. *
  153. * @return void
  154. */
  155. public function emptyMethod() {
  156. $this->methodCalls[] = 'emptyMethod';
  157. }
  158. /**
  159. * oneParamMethod method
  160. *
  161. * @param mixed $param
  162. * @return void
  163. */
  164. public function oneParamMethod($param) {
  165. $this->methodCalls[] = array('oneParamMethod' => array($param));
  166. }
  167. /**
  168. * twoParamMethod method
  169. *
  170. * @param mixed $param
  171. * @param mixed $paramTwo
  172. * @return void
  173. */
  174. public function twoParamMethod($param, $paramTwo) {
  175. $this->methodCalls[] = array('twoParamMethod' => array($param, $paramTwo));
  176. }
  177. /**
  178. * threeParamMethod method
  179. *
  180. * @param mixed $param
  181. * @param mixed $paramTwo
  182. * @param mixed $paramThree
  183. * @return void
  184. */
  185. public function threeParamMethod($param, $paramTwo, $paramThree) {
  186. $this->methodCalls[] = array('threeParamMethod' => array($param, $paramTwo, $paramThree));
  187. }
  188. /**
  189. * fourParamMethod method
  190. *
  191. * @param mixed $param
  192. * @param mixed $paramTwo
  193. * @param mixed $paramThree
  194. * @param mixed $paramFour
  195. * @return void
  196. */
  197. public function fourParamMethod($param, $paramTwo, $paramThree, $paramFour) {
  198. $this->methodCalls[] = array('fourParamMethod' => array($param, $paramTwo, $paramThree, $paramFour));
  199. }
  200. /**
  201. * fiveParamMethod method
  202. *
  203. * @param mixed $param
  204. * @param mixed $paramTwo
  205. * @param mixed $paramThree
  206. * @param mixed $paramFour
  207. * @param mixed $paramFive
  208. * @return void
  209. */
  210. public function fiveParamMethod($param, $paramTwo, $paramThree, $paramFour, $paramFive) {
  211. $this->methodCalls[] = array('fiveParamMethod' => array($param, $paramTwo, $paramThree, $paramFour, $paramFive));
  212. }
  213. /**
  214. * crazyMethod method
  215. *
  216. * @param mixed $param
  217. * @param mixed $paramTwo
  218. * @param mixed $paramThree
  219. * @param mixed $paramFour
  220. * @param mixed $paramFive
  221. * @param mixed $paramSix
  222. * @param mixed $paramSeven
  223. * @return void
  224. */
  225. public function crazyMethod($param, $paramTwo, $paramThree, $paramFour, $paramFive, $paramSix, $paramSeven = null) {
  226. $this->methodCalls[] = array('crazyMethod' => array($param, $paramTwo, $paramThree, $paramFour, $paramFive, $paramSix, $paramSeven));
  227. }
  228. /**
  229. * methodWithOptionalParam method
  230. *
  231. * @param mixed $param
  232. * @return void
  233. */
  234. public function methodWithOptionalParam($param = null) {
  235. $this->methodCalls[] = array('methodWithOptionalParam' => array($param));
  236. }
  237. /**
  238. * undocumented function
  239. *
  240. * @return void
  241. */
  242. public function set($properties = array()) {
  243. return parent::_set($properties);
  244. }
  245. }
  246. /**
  247. * ObjectTestModel class
  248. *
  249. * @package Cake.Test.Case.Core
  250. */
  251. class ObjectTestModel extends CakeTestModel {
  252. public $useTable = false;
  253. public $name = 'ObjectTestModel';
  254. }
  255. /**
  256. * Object Test class
  257. *
  258. * @package Cake.Test.Case.Core
  259. */
  260. class ObjectTest extends CakeTestCase {
  261. /**
  262. * fixtures
  263. *
  264. * @var string
  265. */
  266. public $fixtures = array('core.post', 'core.test_plugin_comment', 'core.comment');
  267. /**
  268. * setUp method
  269. *
  270. * @return void
  271. */
  272. public function setUp() {
  273. parent::setUp();
  274. $this->object = new TestObject();
  275. }
  276. /**
  277. * tearDown method
  278. *
  279. * @return void
  280. */
  281. public function tearDown() {
  282. parent::tearDown();
  283. CakePlugin::unload();
  284. unset($this->object);
  285. }
  286. /**
  287. * testLog method
  288. *
  289. * @return void
  290. */
  291. public function testLog() {
  292. if (file_exists(LOGS . 'error.log')) {
  293. unlink(LOGS . 'error.log');
  294. }
  295. $this->assertTrue($this->object->log('Test warning 1'));
  296. $this->assertTrue($this->object->log(array('Test' => 'warning 2')));
  297. $result = file(LOGS . 'error.log');
  298. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Error: Test warning 1$/', $result[0]);
  299. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Error: Array$/', $result[1]);
  300. $this->assertRegExp('/^\($/', $result[2]);
  301. $this->assertRegExp('/\[Test\] => warning 2$/', $result[3]);
  302. $this->assertRegExp('/^\)$/', $result[4]);
  303. unlink(LOGS . 'error.log');
  304. $this->assertTrue($this->object->log('Test warning 1', LOG_WARNING));
  305. $this->assertTrue($this->object->log(array('Test' => 'warning 2'), LOG_WARNING));
  306. $result = file(LOGS . 'error.log');
  307. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 1$/', $result[0]);
  308. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Array$/', $result[1]);
  309. $this->assertRegExp('/^\($/', $result[2]);
  310. $this->assertRegExp('/\[Test\] => warning 2$/', $result[3]);
  311. $this->assertRegExp('/^\)$/', $result[4]);
  312. unlink(LOGS . 'error.log');
  313. }
  314. /**
  315. * testSet method
  316. *
  317. * @return void
  318. */
  319. public function testSet() {
  320. $this->object->set('a string');
  321. $this->assertEquals('Joel', $this->object->firstName);
  322. $this->object->set(array('firstName'));
  323. $this->assertEquals('Joel', $this->object->firstName);
  324. $this->object->set(array('firstName' => 'Ashley'));
  325. $this->assertEquals('Ashley', $this->object->firstName);
  326. $this->object->set(array('firstName' => 'Joel', 'lastName' => 'Moose'));
  327. $this->assertEquals('Joel', $this->object->firstName);
  328. $this->assertEquals('Moose', $this->object->lastName);
  329. }
  330. /**
  331. * testToString method
  332. *
  333. * @return void
  334. */
  335. public function testToString() {
  336. $result = strtolower($this->object->toString());
  337. $this->assertEquals('testobject', $result);
  338. }
  339. /**
  340. * testMethodDispatching method
  341. *
  342. * @return void
  343. */
  344. public function testMethodDispatching() {
  345. $this->object->emptyMethod();
  346. $expected = array('emptyMethod');
  347. $this->assertSame($this->object->methodCalls, $expected);
  348. $this->object->oneParamMethod('Hello');
  349. $expected[] = array('oneParamMethod' => array('Hello'));
  350. $this->assertSame($this->object->methodCalls, $expected);
  351. $this->object->twoParamMethod(true, false);
  352. $expected[] = array('twoParamMethod' => array(true, false));
  353. $this->assertSame($this->object->methodCalls, $expected);
  354. $this->object->threeParamMethod(true, false, null);
  355. $expected[] = array('threeParamMethod' => array(true, false, null));
  356. $this->assertSame($this->object->methodCalls, $expected);
  357. $this->object->crazyMethod(1, 2, 3, 4, 5, 6, 7);
  358. $expected[] = array('crazyMethod' => array(1, 2, 3, 4, 5, 6, 7));
  359. $this->assertSame($this->object->methodCalls, $expected);
  360. $this->object = new TestObject();
  361. $this->assertSame($this->object->methodCalls, array());
  362. $this->object->dispatchMethod('emptyMethod');
  363. $expected = array('emptyMethod');
  364. $this->assertSame($this->object->methodCalls, $expected);
  365. $this->object->dispatchMethod('oneParamMethod', array('Hello'));
  366. $expected[] = array('oneParamMethod' => array('Hello'));
  367. $this->assertSame($this->object->methodCalls, $expected);
  368. $this->object->dispatchMethod('twoParamMethod', array(true, false));
  369. $expected[] = array('twoParamMethod' => array(true, false));
  370. $this->assertSame($this->object->methodCalls, $expected);
  371. $this->object->dispatchMethod('threeParamMethod', array(true, false, null));
  372. $expected[] = array('threeParamMethod' => array(true, false, null));
  373. $this->assertSame($this->object->methodCalls, $expected);
  374. $this->object->dispatchMethod('fourParamMethod', array(1, 2, 3, 4));
  375. $expected[] = array('fourParamMethod' => array(1, 2, 3, 4));
  376. $this->assertSame($this->object->methodCalls, $expected);
  377. $this->object->dispatchMethod('fiveParamMethod', array(1, 2, 3, 4, 5));
  378. $expected[] = array('fiveParamMethod' => array(1, 2, 3, 4, 5));
  379. $this->assertSame($this->object->methodCalls, $expected);
  380. $this->object->dispatchMethod('crazyMethod', array(1, 2, 3, 4, 5, 6, 7));
  381. $expected[] = array('crazyMethod' => array(1, 2, 3, 4, 5, 6, 7));
  382. $this->assertSame($this->object->methodCalls, $expected);
  383. $this->object->dispatchMethod('methodWithOptionalParam', array('Hello'));
  384. $expected[] = array('methodWithOptionalParam' => array("Hello"));
  385. $this->assertSame($this->object->methodCalls, $expected);
  386. $this->object->dispatchMethod('methodWithOptionalParam');
  387. $expected[] = array('methodWithOptionalParam' => array(null));
  388. $this->assertSame($this->object->methodCalls, $expected);
  389. }
  390. /**
  391. * testRequestAction method
  392. *
  393. * @return void
  394. */
  395. public function testRequestAction() {
  396. App::build(array(
  397. 'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS),
  398. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS),
  399. 'Controller' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Controller' . DS)
  400. ), App::RESET);
  401. $this->assertNull(Router::getRequest(), 'request stack should be empty.');
  402. $result = $this->object->requestAction('');
  403. $this->assertFalse($result);
  404. $result = $this->object->requestAction('/request_action/test_request_action');
  405. $expected = 'This is a test';
  406. $this->assertEquals($expected, $result);
  407. $result = $this->object->requestAction(
  408. Configure::read('App.fullBaseURL') . '/request_action/test_request_action'
  409. );
  410. $expected = 'This is a test';
  411. $this->assertEquals($expected, $result);
  412. $result = $this->object->requestAction('/request_action/another_ra_test/2/5');
  413. $expected = 7;
  414. $this->assertEquals($expected, $result);
  415. $result = $this->object->requestAction('/tests_apps/index', array('return'));
  416. $expected = 'This is the TestsAppsController index view ';
  417. $this->assertEquals($expected, $result);
  418. $result = $this->object->requestAction('/tests_apps/some_method');
  419. $expected = 5;
  420. $this->assertEquals($expected, $result);
  421. $result = $this->object->requestAction('/request_action/paginate_request_action');
  422. $this->assertTrue($result);
  423. $result = $this->object->requestAction('/request_action/normal_request_action');
  424. $expected = 'Hello World';
  425. $this->assertEquals($expected, $result);
  426. $this->assertNull(Router::getRequest(), 'requests were not popped off the stack, this will break url generation');
  427. }
  428. /**
  429. * test requestAction() and plugins.
  430. *
  431. * @return void
  432. */
  433. public function testRequestActionPlugins() {
  434. App::build(array(
  435. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
  436. ), App::RESET);
  437. CakePlugin::load('TestPlugin');
  438. Router::reload();
  439. $result = $this->object->requestAction('/test_plugin/tests/index', array('return'));
  440. $expected = 'test plugin index';
  441. $this->assertEquals($expected, $result);
  442. $result = $this->object->requestAction('/test_plugin/tests/index/some_param', array('return'));
  443. $expected = 'test plugin index';
  444. $this->assertEquals($expected, $result);
  445. $result = $this->object->requestAction(
  446. array('controller' => 'tests', 'action' => 'index', 'plugin' => 'test_plugin'), array('return')
  447. );
  448. $expected = 'test plugin index';
  449. $this->assertEquals($expected, $result);
  450. $result = $this->object->requestAction('/test_plugin/tests/some_method');
  451. $expected = 25;
  452. $this->assertEquals($expected, $result);
  453. $result = $this->object->requestAction(
  454. array('controller' => 'tests', 'action' => 'some_method', 'plugin' => 'test_plugin')
  455. );
  456. $expected = 25;
  457. $this->assertEquals($expected, $result);
  458. }
  459. /**
  460. * test requestAction() with arrays.
  461. *
  462. * @return void
  463. */
  464. public function testRequestActionArray() {
  465. App::build(array(
  466. 'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS),
  467. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS),
  468. 'Controller' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Controller' . DS),
  469. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  470. ), App::RESET);
  471. CakePlugin::load(array('TestPlugin'));
  472. $result = $this->object->requestAction(
  473. array('controller' => 'request_action', 'action' => 'test_request_action')
  474. );
  475. $expected = 'This is a test';
  476. $this->assertEquals($expected, $result);
  477. $result = $this->object->requestAction(
  478. array('controller' => 'request_action', 'action' => 'another_ra_test'),
  479. array('pass' => array('5', '7'))
  480. );
  481. $expected = 12;
  482. $this->assertEquals($expected, $result);
  483. $result = $this->object->requestAction(
  484. array('controller' => 'tests_apps', 'action' => 'index'), array('return')
  485. );
  486. $expected = 'This is the TestsAppsController index view ';
  487. $this->assertEquals($expected, $result);
  488. $result = $this->object->requestAction(array('controller' => 'tests_apps', 'action' => 'some_method'));
  489. $expected = 5;
  490. $this->assertEquals($expected, $result);
  491. $result = $this->object->requestAction(
  492. array('controller' => 'request_action', 'action' => 'normal_request_action')
  493. );
  494. $expected = 'Hello World';
  495. $this->assertEquals($expected, $result);
  496. $result = $this->object->requestAction(
  497. array('controller' => 'request_action', 'action' => 'paginate_request_action')
  498. );
  499. $this->assertTrue($result);
  500. $result = $this->object->requestAction(
  501. array('controller' => 'request_action', 'action' => 'paginate_request_action'),
  502. array('pass' => array(5), 'named' => array('param' => 'value'))
  503. );
  504. $this->assertTrue($result);
  505. }
  506. /**
  507. * Test that requestAction() does not forward the 0 => return value.
  508. *
  509. * @return void
  510. */
  511. public function testRequestActionRemoveReturnParam() {
  512. $result = $this->object->requestAction(
  513. '/request_action/param_check', array('return')
  514. );
  515. $this->assertEquals('', $result, 'Return key was found');
  516. }
  517. /**
  518. * Test that requestAction() is populating $this->params properly
  519. *
  520. * @return void
  521. */
  522. public function testRequestActionParamParseAndPass() {
  523. $result = $this->object->requestAction('/request_action/params_pass');
  524. $this->assertEquals('request_action/params_pass', $result->url);
  525. $this->assertEquals('request_action', $result['controller']);
  526. $this->assertEquals('params_pass', $result['action']);
  527. $this->assertEquals(null, $result['plugin']);
  528. $result = $this->object->requestAction('/request_action/params_pass/sort:desc/limit:5');
  529. $expected = array('sort' => 'desc', 'limit' => 5,);
  530. $this->assertEquals($expected, $result['named']);
  531. $result = $this->object->requestAction(
  532. array('controller' => 'request_action', 'action' => 'params_pass'),
  533. array('named' => array('sort' => 'desc', 'limit' => 5))
  534. );
  535. $this->assertEquals($expected, $result['named']);
  536. }
  537. /**
  538. * Test that requestAction handles get parameters correctly.
  539. *
  540. * @return void
  541. */
  542. public function testRequestActionGetParameters() {
  543. $result = $this->object->requestAction(
  544. '/request_action/params_pass?get=value&limit=5'
  545. );
  546. $this->assertEquals('value', $result->query['get']);
  547. $result = $this->object->requestAction(
  548. array('controller' => 'request_action', 'action' => 'params_pass'),
  549. array('url' => array('get' => 'value', 'limit' => 5))
  550. );
  551. $this->assertEquals('value', $result->query['get']);
  552. }
  553. /**
  554. * test that requestAction does not fish data out of the POST
  555. * superglobal.
  556. *
  557. * @return void
  558. */
  559. public function testRequestActionNoPostPassing() {
  560. $_tmp = $_POST;
  561. $_POST = array('data' => array(
  562. 'item' => 'value'
  563. ));
  564. $result = $this->object->requestAction(array('controller' => 'request_action', 'action' => 'post_pass'));
  565. $this->assertEmpty($result);
  566. $result = $this->object->requestAction(
  567. array('controller' => 'request_action', 'action' => 'post_pass'),
  568. array('data' => $_POST['data'])
  569. );
  570. $expected = $_POST['data'];
  571. $this->assertEquals($expected, $result);
  572. $result = $this->object->requestAction('/request_action/post_pass');
  573. $expected = $_POST['data'];
  574. $this->assertEquals($expected, $result);
  575. $_POST = $_tmp;
  576. }
  577. /**
  578. * Test requestAction with post data.
  579. *
  580. * @return void
  581. */
  582. public function testRequestActionPostWithData() {
  583. $data = array(
  584. 'Post' => array('id' => 2)
  585. );
  586. $result = $this->object->requestAction(
  587. array('controller' => 'request_action', 'action' => 'post_pass'),
  588. array('data' => $data)
  589. );
  590. $this->assertEquals($data, $result);
  591. $result = $this->object->requestAction(
  592. '/request_action/post_pass',
  593. array('data' => $data)
  594. );
  595. $this->assertEquals($data, $result);
  596. }
  597. }