LinkableBehaviorTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. <?php
  2. App::uses('Model', 'Model');
  3. App::uses('Controller', 'Controller');
  4. class LinkableBehaviorTest extends CakeTestCase {
  5. public $fixtures = array(
  6. 'plugin.tools.linkable_user',
  7. 'plugin.tools.linkable_profile',
  8. 'plugin.tools.generic',
  9. 'plugin.tools.linkable_comment',
  10. 'plugin.tools.blog_post',
  11. 'plugin.tools.blog_posts_linkable_tag',
  12. 'plugin.tools.linkable_tag',
  13. 'plugin.tools.legacy_product',
  14. 'plugin.tools.legacy_company',
  15. 'plugin.tools.shipment',
  16. 'plugin.tools.order_item',
  17. 'plugin.tools.news_article',
  18. 'plugin.tools.news_category',
  19. 'plugin.tools.news_articles_news_category',
  20. );
  21. public $User;
  22. public function setUp() {
  23. parent::setUp();
  24. $this->User = ClassRegistry::init('LinkableUser');
  25. }
  26. public function tearDown() {
  27. parent::tearDown();
  28. unset($this->User);
  29. }
  30. public function testBelongsTo() {
  31. $arrayExpected = array(
  32. 'LinkableUser' => array('id' => 1, 'username' => 'CakePHP'),
  33. 'LinkableProfile' => array('id' => 1, 'user_id' => 1, 'biography' => 'CakePHP is a rapid development framework for PHP that provides an extensible architecture for developing, maintaining, and deploying applications.')
  34. );
  35. $arrayResult = $this->User->find('first', array(
  36. 'contain' => array(
  37. 'LinkableProfile'
  38. )
  39. ));
  40. $arrayExpectedTmp = $arrayExpected;
  41. $arrayExpectedTmp['LinkableUser']['role_id'] = 1;
  42. $this->assertTrue(isset($arrayResult['LinkableProfile']), 'belongsTo association via Containable: %s');
  43. $this->assertEquals($arrayExpectedTmp, $arrayResult, 'belongsTo association via Containable: %s');
  44. // Same association, but this time with Linkable
  45. $arrayResult = $this->User->find('first', array(
  46. 'fields' => array(
  47. 'id',
  48. 'username'
  49. ),
  50. 'contain' => false,
  51. 'link' => array(
  52. 'LinkableProfile' => array(
  53. 'fields' => array(
  54. 'id',
  55. 'user_id',
  56. 'biography'
  57. )
  58. )
  59. )
  60. ));
  61. $this->assertTrue(isset($arrayResult['LinkableProfile']), 'belongsTo association via Linkable: %s');
  62. $this->assertTrue(!empty($arrayResult['LinkableProfile']), 'belongsTo association via Linkable: %s');
  63. $this->assertEquals($arrayExpected, $arrayResult, 'belongsTo association via Linkable: %s');
  64. // Linkable association, no field lists
  65. $arrayResult = $this->User->find('first', array(
  66. 'contain' => false,
  67. 'link' => array(
  68. 'LinkableProfile'
  69. )
  70. ));
  71. $arrayExpectedTmp = $arrayExpected;
  72. $arrayExpectedTmp['LinkableUser']['role_id'] = 1;
  73. $this->assertTrue(isset($arrayResult['LinkableProfile']), 'belongsTo association via Linkable (automatic fields): %s');
  74. $this->assertEquals($arrayExpectedTmp, $arrayResult, 'belongsTo association via Linkable (automatic fields): %s');
  75. // On-the-fly association via Linkable
  76. $arrayExpected = array(
  77. 'LinkableUser' => array('id' => 1, 'username' => 'CakePHP'),
  78. 'Generic' => array('id' => 1, 'text' => '')
  79. );
  80. $arrayResult = $this->User->find('first', array(
  81. 'contain' => false,
  82. 'link' => array(
  83. 'Generic' => array(
  84. 'class' => 'Generic',
  85. 'conditions' => array('exactly' => 'LinkableUser.id = Generic.id'),
  86. 'fields' => array(
  87. 'id',
  88. 'text'
  89. )
  90. )
  91. )
  92. ));
  93. $arrayExpectedTmp = $arrayExpected;
  94. $arrayExpectedTmp['LinkableUser']['role_id'] = 1;
  95. $this->assertTrue(isset($arrayResult['Generic']), 'On-the-fly belongsTo association via Linkable: %s');
  96. $this->assertEquals($arrayExpectedTmp, $arrayResult, 'On-the-fly belongsTo association via Linkable: %s');
  97. // On-the-fly association via Linkable, with order on the associations' row and using array conditions instead of plain string
  98. $arrayExpected = array(
  99. 'LinkableUser' => array('id' => 4, 'username' => 'CodeIgniter'),
  100. 'Generic' => array('id' => 4, 'text' => '')
  101. );
  102. $arrayResult = $this->User->find('first', array(
  103. 'contain' => false,
  104. 'link' => array(
  105. 'Generic' => array(
  106. 'class' => 'Generic',
  107. 'conditions' => array('exactly' => array('LinkableUser.id = Generic.id')),
  108. 'fields' => array(
  109. 'id',
  110. 'text'
  111. )
  112. )
  113. ),
  114. 'order' => 'Generic.id DESC'
  115. ));
  116. $arrayExpectedTmp = $arrayExpected;
  117. $arrayExpectedTmp['LinkableUser']['role_id'] = 3;
  118. $this->assertEquals($arrayExpectedTmp, $arrayResult, 'On-the-fly belongsTo association via Linkable, with order: %s');
  119. }
  120. /**
  121. * hasMany association via Containable. Should still work when Linkable is loaded.
  122. *
  123. * @return void
  124. */
  125. public function testHasMany() {
  126. $arrayExpected = array(
  127. 'LinkableUser' => array('id' => 1, 'username' => 'CakePHP'),
  128. 'LinkableComment' => array(
  129. 0 => array(
  130. 'id' => 1,
  131. 'user_id' => 1,
  132. 'body' => 'Text'
  133. ),
  134. 1 => array(
  135. 'id' => 2,
  136. 'user_id' => 1,
  137. 'body' => 'Text'
  138. ),
  139. )
  140. );
  141. $arrayResult = $this->User->find('first', array(
  142. 'contain' => array(
  143. 'LinkableComment'
  144. ),
  145. 'order' => 'LinkableUser.id ASC'
  146. ));
  147. $arrayExpectedTmp = $arrayExpected;
  148. $arrayExpectedTmp['LinkableUser']['role_id'] = 1;
  149. $this->assertTrue(isset($arrayResult['LinkableComment']), 'hasMany association via Containable: %s');
  150. $this->assertEquals($arrayExpectedTmp, $arrayResult, 'hasMany association via Containable: %s');
  151. // Same association, but this time with Linkable
  152. $arrayExpected = array(
  153. 'LinkableUser' => array('id' => 1, 'username' => 'CakePHP'),
  154. 'LinkableComment' => array(
  155. 'id' => 1,
  156. 'user_id' => 1,
  157. 'body' => 'Text'
  158. )
  159. );
  160. $arrayResult = $this->User->find('first', array(
  161. 'fields' => array(
  162. 'id',
  163. 'username'
  164. ),
  165. 'contain' => false,
  166. 'link' => array(
  167. 'LinkableComment' => array(
  168. 'fields' => array(
  169. 'id',
  170. 'user_id',
  171. 'body'
  172. )
  173. )
  174. ),
  175. 'order' => 'LinkableUser.id ASC',
  176. 'group' => 'LinkableUser.id'
  177. ));
  178. $this->assertEquals($arrayExpected, $arrayResult, 'hasMany association via Linkable: %s');
  179. }
  180. public function testComplexAssociations() {
  181. $this->BlogPost = ClassRegistry::init('BlogPost');
  182. $arrayExpected = array(
  183. 'BlogPost' => array('id' => 1, 'title' => 'Post 1', 'user_id' => 1),
  184. 'LinkableTag' => array('name' => 'General'),
  185. 'LinkableProfile' => array('biography' => 'CakePHP is a rapid development framework for PHP that provides an extensible architecture for developing, maintaining, and deploying applications.'),
  186. 'MainLinkableTag' => array('name' => 'General'),
  187. 'Generic' => array('id' => 1,'text' => ''),
  188. 'LinkableUser' => array('id' => 1, 'username' => 'CakePHP')
  189. );
  190. $arrayResult = $this->BlogPost->find('first', array(
  191. 'conditions' => array(
  192. 'MainLinkableTag.id' => 1
  193. ),
  194. 'link' => array(
  195. 'LinkableUser' => array(
  196. 'LinkableProfile' => array(
  197. 'fields' => array(
  198. 'biography'
  199. ),
  200. 'Generic' => array(
  201. 'class' => 'Generic',
  202. 'conditions' => array('exactly' => 'LinkableUser.id = Generic.id'),
  203. )
  204. )
  205. ),
  206. 'LinkableTag' => array(
  207. 'table' => 'linkable_tags',
  208. 'fields' => array(
  209. 'name'
  210. )
  211. ),
  212. 'MainLinkableTag' => array(
  213. 'class' => 'LinkableTag',
  214. 'conditions' => array('exactly' => 'BlogPostsLinkableTag.blog_post_id = BlogPost.id'),
  215. 'fields' => array(
  216. 'MainLinkableTag.name'
  217. )
  218. )
  219. )
  220. ));
  221. $arrayExpectedTmp = $arrayExpected;
  222. $arrayExpectedTmp['LinkableUser']['role_id'] = 1;
  223. $this->assertEquals($arrayExpectedTmp, $arrayResult, 'Complex find: %s');
  224. // Linkable and Containable combined
  225. $arrayExpected = array(
  226. 'BlogPost' => array('id' => 1, 'title' => 'Post 1', 'user_id' => 1),
  227. 'LinkableUser' => array('id' => 1, 'username' => 'CakePHP'),
  228. 'LinkableTag' => array(
  229. array('id' => 1, 'name' => 'General', 'parent_id' => null, 'BlogPostsLinkableTag' => array('id' => 1, 'blog_post_id' => 1, 'tag_id' => 1, 'main' => 0)),
  230. //array('id' => 2, 'name' => 'Test I', 'parent_id' => 1, 'BlogPostsLinkableTag' => array('id' => 2, 'blog_post_id' => 1, 'tag_id' => 2, 'main' => 1))
  231. ),
  232. );
  233. $arrayResult = $this->BlogPost->find('first', array(
  234. 'contain' => array(
  235. 'LinkableTag'
  236. ),
  237. 'link' => array(
  238. 'LinkableUser'
  239. )
  240. ));
  241. $arrayExpectedTmp = $arrayExpected;
  242. $arrayExpectedTmp['LinkableUser']['role_id'] = 1;
  243. $this->assertEquals($arrayExpectedTmp, $arrayResult, 'Linkable and Containable combined: %s');
  244. }
  245. public function testPagination() {
  246. $objController = new Controller(new CakeRequest(), new CakeResponse());
  247. $objController->layout = 'ajax';
  248. $objController->uses = array('LinkableUser');
  249. $objController->constructClasses();
  250. $objController->request->url = '/';
  251. $objController->paginate = array(
  252. 'fields' => array(
  253. 'username'
  254. ),
  255. 'contain' => false,
  256. 'link' => array(
  257. 'LinkableProfile' => array(
  258. 'fields' => array(
  259. 'biography'
  260. )
  261. )
  262. ),
  263. 'limit' => 2
  264. );
  265. $arrayResult = $objController->paginate('LinkableUser');
  266. $this->assertEquals(4, $objController->params['paging']['LinkableUser']['count'], 'Paging: total records count: %s');
  267. // Pagination with order on a row from table joined with Linkable
  268. $objController->paginate = array(
  269. 'fields' => array(
  270. 'id'
  271. ),
  272. 'contain' => false,
  273. 'link' => array(
  274. 'LinkableProfile' => array(
  275. 'fields' => array(
  276. 'user_id'
  277. )
  278. )
  279. ),
  280. 'limit' => 2,
  281. 'order' => 'LinkableProfile.user_id DESC'
  282. );
  283. $arrayResult = $objController->paginate('LinkableUser');
  284. $arrayExpected = array(
  285. 0 => array(
  286. 'LinkableUser' => array(
  287. 'id' => 4
  288. ),
  289. 'LinkableProfile' => array('user_id' => 4)
  290. ),
  291. 1 => array(
  292. 'LinkableUser' => array(
  293. 'id' => 3
  294. ),
  295. 'LinkableProfile' => array('user_id' => 3)
  296. )
  297. );
  298. $this->assertEquals($arrayExpected, $arrayResult, 'Paging with order on join table row: %s');
  299. // Pagination without specifying any fields
  300. $objController->paginate = array(
  301. 'contain' => false,
  302. 'link' => array(
  303. 'LinkableProfile'
  304. ),
  305. 'limit' => 2,
  306. 'order' => 'LinkableProfile.user_id DESC'
  307. );
  308. $arrayResult = $objController->paginate('LinkableUser');
  309. $this->assertEquals(4, $objController->params['paging']['LinkableUser']['count'], 'Paging without any field lists: total records count: %s');
  310. }
  311. /**
  312. * Series of tests that assert if Linkable can adapt to assocations that
  313. * have aliases different from their standard model names.
  314. *
  315. * @return void
  316. */
  317. public function testNonstandardAssociationNames() {
  318. $this->LinkableTag = ClassRegistry::init('LinkableTag');
  319. $arrayExpected = array(
  320. 'LinkableTag' => array(
  321. 'name' => 'Test I'
  322. ),
  323. 'Parent' => array(
  324. 'name' => 'General'
  325. )
  326. );
  327. $arrayResult = $this->LinkableTag->find('first', array(
  328. 'fields' => array(
  329. 'name'
  330. ),
  331. 'conditions' => array(
  332. 'LinkableTag.id' => 2
  333. ),
  334. 'link' => array(
  335. 'Parent' => array(
  336. 'fields' => array(
  337. 'name'
  338. )
  339. )
  340. )
  341. ));
  342. $this->assertEquals($arrayExpected, $arrayResult, 'Association with non-standard name: %s');
  343. $this->LegacyProduct = ClassRegistry::init('LegacyProduct');
  344. $arrayExpected = array(
  345. 'LegacyProduct' => array(
  346. 'name' => 'Velocipede'
  347. ),
  348. 'Maker' => array(
  349. 'company_name' => 'Vintage Stuff Manufactory'
  350. ),
  351. 'Transporter' => array(
  352. 'company_name' => 'Joe & Co Crate Shipping Company'
  353. )
  354. );
  355. $arrayResult = $this->LegacyProduct->find('first', array(
  356. 'fields' => array(
  357. 'name'
  358. ),
  359. 'conditions' => array(
  360. 'LegacyProduct.product_id' => 1
  361. ),
  362. 'link' => array(
  363. 'Maker' => array(
  364. 'fields' => array(
  365. 'company_name'
  366. )
  367. ),
  368. 'Transporter' => array(
  369. 'fields' => array(
  370. 'company_name'
  371. )
  372. )
  373. )
  374. ));
  375. $this->assertEquals($arrayExpected, $arrayResult, 'belongsTo associations with custom foreignKey: %s');
  376. $arrayExpected = array(
  377. 'ProductsMade' => array(
  378. 'name' => 'Velocipede'
  379. ),
  380. 'Maker' => array(
  381. 'company_name' => 'Vintage Stuff Manufactory'
  382. )
  383. );
  384. $arrayResult = $this->LegacyProduct->Maker->find('first', array(
  385. 'fields' => array(
  386. 'company_name'
  387. ),
  388. 'conditions' => array(
  389. 'Maker.company_id' => 1
  390. ),
  391. 'link' => array(
  392. 'ProductsMade' => array(
  393. 'fields' => array(
  394. 'name'
  395. )
  396. )
  397. )
  398. ));
  399. $this->assertEquals($arrayExpected, $arrayResult, 'hasMany association with custom foreignKey: %s');
  400. }
  401. public function testAliasedBelongsToWithSameModelAsHasMany() {
  402. $this->OrderItem = ClassRegistry::init('OrderItem');
  403. $arrayExpected = array(
  404. 0 => array(
  405. 'OrderItem' => array(
  406. 'id' => 50,
  407. 'active_shipment_id' => 320
  408. ),
  409. 'ActiveShipment' => array(
  410. 'id' => 320,
  411. 'ship_date' => '2011-01-07',
  412. 'order_item_id' => 50
  413. )
  414. )
  415. );
  416. $arrayResult = $this->OrderItem->find('all', array(
  417. 'recursive' => -1,
  418. 'conditions' => array(
  419. 'ActiveShipment.ship_date' => date('2011-01-07'),
  420. ),
  421. 'link' => array('ActiveShipment'),
  422. ));
  423. $this->assertEquals($arrayExpected, $arrayResult, 'belongsTo association with alias (requested), with hasMany to the same model without alias: %s');
  424. }
  425. /**
  426. * Ensure that the correct habtm keys are read from the relationship in the models
  427. *
  428. * @author David Yell <neon1024@gmail.com>
  429. * @return void
  430. */
  431. public function testHasAndBelongsToManyNonConvention() {
  432. $this->NewsArticle = ClassRegistry::init('NewsArticle');
  433. $expected = array(
  434. array(
  435. 'NewsArticle' => array(
  436. 'id' => '1',
  437. 'title' => 'CakePHP the best framework'
  438. ),
  439. 'NewsCategory' => array(
  440. 'id' => '1',
  441. 'name' => 'Development'
  442. )
  443. )
  444. );
  445. $result = $this->NewsArticle->find('all', array(
  446. 'link' => array(
  447. 'NewsCategory'
  448. ),
  449. 'conditions' => array(
  450. 'NewsCategory.id' => 1
  451. )
  452. ));
  453. $this->assertEqual($result, $expected);
  454. }
  455. }
  456. class LinkableTestModel extends CakeTestModel {
  457. public $recursive = -1;
  458. public $actsAs = array(
  459. 'Containable',
  460. 'Tools.Linkable',
  461. );
  462. }
  463. class LinkableUser extends LinkableTestModel {
  464. public $hasOne = array(
  465. 'LinkableProfile' => array(
  466. 'className' => 'LinkableProfile',
  467. 'foreignKey' => 'user_id'
  468. )
  469. );
  470. public $hasMany = array(
  471. 'LinkableComment' => array(
  472. 'className' => 'LinkableComment',
  473. 'foreignKey' => 'user_id'
  474. ),
  475. 'BlogPost' => array(
  476. 'foreignKey' => 'user_id'
  477. )
  478. );
  479. }
  480. class LinkableComment extends LinkableTestModel {
  481. }
  482. class LinkableProfile extends LinkableTestModel {
  483. public $belongsTo = array(
  484. 'LinkableUser' => array(
  485. 'className' => 'LinkableUser',
  486. 'foreignKey' => 'user_id'
  487. )
  488. );
  489. }
  490. class BlogPost extends LinkableTestModel {
  491. public $belongsTo = array(
  492. 'LinkableUser' => array(
  493. 'className' => 'LinkableUser',
  494. 'foreignKey' => 'user_id'
  495. ),
  496. );
  497. public $hasAndBelongsToMany = array(
  498. 'LinkableTag' => array(
  499. 'foreignKey' => 'tag_id',
  500. 'associationForeignKey' => 'blog_post_id',
  501. )
  502. );
  503. }
  504. class BlogPostLinkableTag extends LinkableTestModel {
  505. }
  506. class LinkableTag extends LinkableTestModel {
  507. public $hasAndBelongsToMany = array(
  508. 'BlogPost' => array(
  509. 'foreignKey' => 'blog_post_id',
  510. 'associationForeignKey' => 'tag_id',
  511. )
  512. );
  513. public $belongsTo = array(
  514. 'Parent' => array(
  515. 'className' => 'LinkableTag',
  516. 'foreignKey' => 'parent_id'
  517. )
  518. );
  519. }
  520. class LegacyProduct extends LinkableTestModel {
  521. public $primaryKey = 'product_id';
  522. public $belongsTo = array(
  523. 'Maker' => array(
  524. 'className' => 'LegacyCompany',
  525. 'foreignKey' => 'the_company_that_builds_it_id'
  526. ),
  527. 'Transporter' => array(
  528. 'className' => 'LegacyCompany',
  529. 'foreignKey' => 'the_company_that_delivers_it_id'
  530. )
  531. );
  532. }
  533. class LegacyCompany extends LinkableTestModel {
  534. public $primaryKey = 'company_id';
  535. public $hasMany = array(
  536. 'ProductsMade' => array(
  537. 'className' => 'LegacyProduct',
  538. 'foreignKey' => 'the_company_that_builds_it_id'
  539. )
  540. );
  541. }
  542. class Shipment extends LinkableTestModel {
  543. public $belongsTo = array(
  544. 'OrderItem'
  545. );
  546. }
  547. class OrderItem extends LinkableTestModel {
  548. public $hasMany = array(
  549. 'Shipment'
  550. );
  551. public $belongsTo = array(
  552. 'ActiveShipment' => array(
  553. 'className' => 'Shipment',
  554. 'foreignKey' => 'active_shipment_id',
  555. ),
  556. );
  557. }
  558. class NewsArticle extends LinkableTestModel {
  559. public $hasAndBelongsToMany = array(
  560. 'NewsCategory' => array(
  561. 'className' => 'NewsCategory',
  562. 'joinTable' => 'news_articles_news_categories',
  563. 'foreignKey' => 'article_id',
  564. 'associationForeignKey' => 'category_id',
  565. 'unique' => 'keepExisting',
  566. )
  567. );
  568. }
  569. class NewsCategory extends LinkableTestModel {
  570. public $hasAndBelongsToMany = array(
  571. 'NewsArticle' => array(
  572. 'className' => 'NewsArticle',
  573. 'joinTable' => 'news_articles_news_categories',
  574. 'foreignKey' => 'category_id',
  575. 'associationForeignKey' => 'article_id',
  576. 'unique' => 'keepExisting',
  577. )
  578. );
  579. }