LinkableBehaviorTest.php 15 KB

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