LinkableBehaviorTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. <?php
  2. App::uses('Model', 'Model');
  3. App::uses('Controller', 'Controller');
  4. class LinkableBehaviorTest extends CakeTestCase {
  5. public $fixtures = [
  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 = [
  32. 'LinkableUser' => ['id' => 1, 'username' => 'CakePHP'],
  33. 'LinkableProfile' => ['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', [
  36. 'contain' => [
  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', [
  46. 'fields' => [
  47. 'id',
  48. 'username'
  49. ],
  50. 'contain' => false,
  51. 'link' => [
  52. 'LinkableProfile' => [
  53. 'fields' => [
  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', [
  66. 'contain' => false,
  67. 'link' => [
  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 = [
  77. 'LinkableUser' => ['id' => 1, 'username' => 'CakePHP'],
  78. 'Generic' => ['id' => 1, 'text' => '']
  79. ];
  80. $arrayResult = $this->User->find('first', [
  81. 'contain' => false,
  82. 'link' => [
  83. 'Generic' => [
  84. 'class' => 'Generic',
  85. 'conditions' => ['exactly' => 'LinkableUser.id = Generic.id'],
  86. 'fields' => [
  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 = [
  99. 'LinkableUser' => ['id' => 4, 'username' => 'CodeIgniter'],
  100. 'Generic' => ['id' => 4, 'text' => '']
  101. ];
  102. $arrayResult = $this->User->find('first', [
  103. 'contain' => false,
  104. 'link' => [
  105. 'Generic' => [
  106. 'class' => 'Generic',
  107. 'conditions' => ['exactly' => ['LinkableUser.id = Generic.id']],
  108. 'fields' => [
  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 = [
  127. 'LinkableUser' => ['id' => 1, 'username' => 'CakePHP'],
  128. 'LinkableComment' => [
  129. 0 => [
  130. 'id' => 1,
  131. 'user_id' => 1,
  132. 'body' => 'Text'
  133. ],
  134. 1 => [
  135. 'id' => 2,
  136. 'user_id' => 1,
  137. 'body' => 'Text'
  138. ],
  139. ]
  140. ];
  141. $arrayResult = $this->User->find('first', [
  142. 'contain' => [
  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 = [
  153. 'LinkableUser' => ['id' => 1, 'username' => 'CakePHP'],
  154. 'LinkableComment' => [
  155. 'id' => 1,
  156. 'user_id' => 1,
  157. 'body' => 'Text'
  158. ]
  159. ];
  160. $arrayResult = $this->User->find('first', [
  161. 'fields' => [
  162. 'id',
  163. 'username'
  164. ],
  165. 'contain' => false,
  166. 'link' => [
  167. 'LinkableComment' => [
  168. 'fields' => [
  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 = [
  183. 'BlogPost' => ['id' => 1, 'title' => 'Post 1', 'user_id' => 1],
  184. 'LinkableTag' => ['name' => 'General'],
  185. 'LinkableProfile' => ['biography' => 'CakePHP is a rapid development framework for PHP that provides an extensible architecture for developing, maintaining, and deploying applications.'],
  186. 'MainLinkableTag' => ['name' => 'General'],
  187. 'Generic' => ['id' => 1, 'text' => ''],
  188. 'LinkableUser' => ['id' => 1, 'username' => 'CakePHP']
  189. ];
  190. $arrayResult = $this->BlogPost->find('first', [
  191. 'conditions' => [
  192. 'MainLinkableTag.id' => 1
  193. ],
  194. 'link' => [
  195. 'LinkableUser' => [
  196. 'LinkableProfile' => [
  197. 'fields' => [
  198. 'biography'
  199. ],
  200. 'Generic' => [
  201. 'class' => 'Generic',
  202. 'conditions' => ['exactly' => 'LinkableUser.id = Generic.id'],
  203. ]
  204. ]
  205. ],
  206. 'LinkableTag' => [
  207. 'table' => 'linkable_tags',
  208. 'fields' => [
  209. 'name'
  210. ]
  211. ],
  212. 'MainLinkableTag' => [
  213. 'class' => 'LinkableTag',
  214. 'conditions' => ['exactly' => 'BlogPostsLinkableTag.blog_post_id = BlogPost.id'],
  215. 'fields' => [
  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 = [
  226. 'BlogPost' => ['id' => 1, 'title' => 'Post 1', 'user_id' => 1],
  227. 'LinkableUser' => ['id' => 1, 'username' => 'CakePHP'],
  228. 'LinkableTag' => [
  229. ['id' => 1, 'name' => 'General', 'parent_id' => null, 'BlogPostsLinkableTag' => ['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', [
  234. 'contain' => [
  235. 'LinkableTag'
  236. ],
  237. 'link' => [
  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 = ['LinkableUser'];
  249. $objController->constructClasses();
  250. $objController->request->url = '/';
  251. $objController->paginate = [
  252. 'fields' => [
  253. 'username'
  254. ],
  255. 'contain' => false,
  256. 'link' => [
  257. 'LinkableProfile' => [
  258. 'fields' => [
  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 = [
  269. 'fields' => [
  270. 'id'
  271. ],
  272. 'contain' => false,
  273. 'link' => [
  274. 'LinkableProfile' => [
  275. 'fields' => [
  276. 'user_id'
  277. ]
  278. ]
  279. ],
  280. 'limit' => 2,
  281. 'order' => 'LinkableProfile.user_id DESC'
  282. ];
  283. $arrayResult = $objController->paginate('LinkableUser');
  284. $arrayExpected = [
  285. 0 => [
  286. 'LinkableUser' => [
  287. 'id' => 4
  288. ],
  289. 'LinkableProfile' => ['user_id' => 4]
  290. ],
  291. 1 => [
  292. 'LinkableUser' => [
  293. 'id' => 3
  294. ],
  295. 'LinkableProfile' => ['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 = [
  301. 'contain' => false,
  302. 'link' => [
  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 = [
  320. 'LinkableTag' => [
  321. 'name' => 'Test I'
  322. ],
  323. 'Parent' => [
  324. 'name' => 'General'
  325. ]
  326. ];
  327. $arrayResult = $this->LinkableTag->find('first', [
  328. 'fields' => [
  329. 'name'
  330. ],
  331. 'conditions' => [
  332. 'LinkableTag.id' => 2
  333. ],
  334. 'link' => [
  335. 'Parent' => [
  336. 'fields' => [
  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 = [
  345. 'LegacyProduct' => [
  346. 'name' => 'Velocipede'
  347. ],
  348. 'Maker' => [
  349. 'company_name' => 'Vintage Stuff Manufactory'
  350. ],
  351. 'Transporter' => [
  352. 'company_name' => 'Joe & Co Crate Shipping Company'
  353. ]
  354. ];
  355. $arrayResult = $this->LegacyProduct->find('first', [
  356. 'fields' => [
  357. 'name'
  358. ],
  359. 'conditions' => [
  360. 'LegacyProduct.product_id' => 1
  361. ],
  362. 'link' => [
  363. 'Maker' => [
  364. 'fields' => [
  365. 'company_name'
  366. ]
  367. ],
  368. 'Transporter' => [
  369. 'fields' => [
  370. 'company_name'
  371. ]
  372. ]
  373. ]
  374. ]);
  375. $this->assertEquals($arrayExpected, $arrayResult, 'belongsTo associations with custom foreignKey: %s');
  376. $arrayExpected = [
  377. 'ProductsMade' => [
  378. 'name' => 'Velocipede'
  379. ],
  380. 'Maker' => [
  381. 'company_name' => 'Vintage Stuff Manufactory'
  382. ]
  383. ];
  384. $arrayResult = $this->LegacyProduct->Maker->find('first', [
  385. 'fields' => [
  386. 'company_name'
  387. ],
  388. 'conditions' => [
  389. 'Maker.company_id' => 1
  390. ],
  391. 'link' => [
  392. 'ProductsMade' => [
  393. 'fields' => [
  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 = [
  404. 0 => [
  405. 'OrderItem' => [
  406. 'id' => 50,
  407. 'active_shipment_id' => 320
  408. ],
  409. 'ActiveShipment' => [
  410. 'id' => 320,
  411. 'ship_date' => '2011-01-07',
  412. 'order_item_id' => 50
  413. ]
  414. ]
  415. ];
  416. $arrayResult = $this->OrderItem->find('all', [
  417. 'recursive' => -1,
  418. 'conditions' => [
  419. 'ActiveShipment.ship_date' => date('2011-01-07'),
  420. ],
  421. 'link' => ['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 = [
  434. [
  435. 'NewsArticle' => [
  436. 'id' => '1',
  437. 'title' => 'CakePHP the best framework'
  438. ],
  439. 'NewsCategory' => [
  440. 'id' => '1',
  441. 'name' => 'Development'
  442. ]
  443. ]
  444. ];
  445. $result = $this->NewsArticle->find('all', [
  446. 'link' => [
  447. 'NewsCategory'
  448. ],
  449. 'conditions' => [
  450. 'NewsCategory.id' => 1
  451. ]
  452. ]);
  453. $this->assertEquals($expected, $result);
  454. }
  455. }
  456. class LinkableTestModel extends CakeTestModel {
  457. public $recursive = -1;
  458. public $actsAs = [
  459. 'Containable',
  460. 'Tools.Linkable',
  461. ];
  462. }
  463. class LinkableUser extends LinkableTestModel {
  464. public $hasOne = [
  465. 'LinkableProfile' => [
  466. 'className' => 'LinkableProfile',
  467. 'foreignKey' => 'user_id'
  468. ]
  469. ];
  470. public $hasMany = [
  471. 'LinkableComment' => [
  472. 'className' => 'LinkableComment',
  473. 'foreignKey' => 'user_id'
  474. ],
  475. 'BlogPost' => [
  476. 'foreignKey' => 'user_id'
  477. ]
  478. ];
  479. }
  480. class LinkableComment extends LinkableTestModel {
  481. }
  482. class LinkableProfile extends LinkableTestModel {
  483. public $belongsTo = [
  484. 'LinkableUser' => [
  485. 'className' => 'LinkableUser',
  486. 'foreignKey' => 'user_id'
  487. ]
  488. ];
  489. }
  490. class BlogPost extends LinkableTestModel {
  491. public $belongsTo = [
  492. 'LinkableUser' => [
  493. 'className' => 'LinkableUser',
  494. 'foreignKey' => 'user_id'
  495. ],
  496. ];
  497. public $hasAndBelongsToMany = [
  498. 'LinkableTag' => [
  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 = [
  508. 'BlogPost' => [
  509. 'foreignKey' => 'blog_post_id',
  510. 'associationForeignKey' => 'tag_id',
  511. ]
  512. ];
  513. public $belongsTo = [
  514. 'Parent' => [
  515. 'className' => 'LinkableTag',
  516. 'foreignKey' => 'parent_id'
  517. ]
  518. ];
  519. }
  520. class LegacyProduct extends LinkableTestModel {
  521. public $primaryKey = 'product_id';
  522. public $belongsTo = [
  523. 'Maker' => [
  524. 'className' => 'LegacyCompany',
  525. 'foreignKey' => 'the_company_that_builds_it_id'
  526. ],
  527. 'Transporter' => [
  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 = [
  536. 'ProductsMade' => [
  537. 'className' => 'LegacyProduct',
  538. 'foreignKey' => 'the_company_that_builds_it_id'
  539. ]
  540. ];
  541. }
  542. class Shipment extends LinkableTestModel {
  543. public $belongsTo = [
  544. 'OrderItem'
  545. ];
  546. }
  547. class OrderItem extends LinkableTestModel {
  548. public $hasMany = [
  549. 'Shipment'
  550. ];
  551. public $belongsTo = [
  552. 'ActiveShipment' => [
  553. 'className' => 'Shipment',
  554. 'foreignKey' => 'active_shipment_id',
  555. ],
  556. ];
  557. }
  558. class NewsArticle extends LinkableTestModel {
  559. public $hasAndBelongsToMany = [
  560. 'NewsCategory' => [
  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 = [
  571. 'NewsArticle' => [
  572. 'className' => 'NewsArticle',
  573. 'joinTable' => 'news_articles_news_categories',
  574. 'foreignKey' => 'category_id',
  575. 'associationForeignKey' => 'article_id',
  576. 'unique' => 'keepExisting',
  577. ]
  578. ];
  579. }