ObjectTest.php 18 KB

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