NamedScopeBehaviorTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. App::uses('NamedScopeBehavior', 'Tools.Model/Behavior');
  3. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  4. class NamedScopeBehaviorTest extends MyCakeTestCase {
  5. public $NamedScopeBehavior;
  6. public $Comment;
  7. public $fixtures = ['core.comment', 'core.user'];
  8. public function setUp() {
  9. parent::setUp();
  10. $this->NamedScopeBehavior = new NamedScopeBehavior();
  11. $this->Comment = ClassRegistry::init('Comment');
  12. $this->Comment->bindModel(['belongsTo' => ['User']], false);
  13. $this->Comment->displayField = 'comment';
  14. $this->Comment->Behaviors->load('Tools.NamedScope');
  15. $this->Comment->User->Behaviors->load('Tools.NamedScope');
  16. }
  17. public function testObject() {
  18. $this->assertTrue(is_object($this->NamedScopeBehavior));
  19. $this->assertInstanceOf('NamedScopeBehavior', $this->NamedScopeBehavior);
  20. }
  21. /**
  22. * NamedScopeBehaviorTest::testScope()
  23. *
  24. * @return void
  25. */
  26. public function testScope() {
  27. $result = $this->Comment->scope('active');
  28. $this->assertNull($result);
  29. $this->Comment->scope('active', ['published' => 'Y']);
  30. $result = $this->Comment->scope('active');
  31. $this->assertEquals(['published' => 'Y'], $result);
  32. $this->Comment->scope('active', ['published' => 'Y', 'active' => 1]);
  33. $result = $this->Comment->scope('active');
  34. $this->assertEquals(['published' => 'Y', 'active' => 1], $result);
  35. $result = $this->Comment->scope();
  36. $this->assertEquals(['active' => ['published' => 'Y', 'active' => 1]], $result);
  37. }
  38. /**
  39. * NamedScopeBehaviorTest::testBasic()
  40. *
  41. * @return void
  42. */
  43. public function testBasic() {
  44. $before = $this->Comment->find('count');
  45. $this->Comment->scope('active', ['published' => 'Y']);
  46. $options = [
  47. 'scope' => ['active']
  48. ];
  49. $after = $this->Comment->find('count', $options);
  50. $this->assertTrue($before > $after);
  51. $this->assertSame(5, $after);
  52. }
  53. /**
  54. * NamedScopeBehaviorTest::testCrossModel()
  55. *
  56. * @return void
  57. */
  58. public function testCrossModel() {
  59. $before = $this->Comment->find('count');
  60. $this->Comment->scope('active', ['Comment.published' => 'Y']);
  61. $this->Comment->User->scope('senior', ['User.id <' => '3']);
  62. $options = [
  63. 'contain' => ['User'],
  64. 'scope' => ['Comment.active', 'User.senior']
  65. ];
  66. $after = $this->Comment->find('count', $options);
  67. $this->assertTrue($before > $after);
  68. $this->assertSame(4, $after);
  69. }
  70. /**
  71. * NamedScopeBehaviorTest::testCrossModelWithAttributeScope()
  72. *
  73. * @return void
  74. */
  75. public function testCrossModelWithAttributeScope() {
  76. $this->Comment->scopes = ['active' => ['Comment.published' => 'Y']];
  77. $this->Comment->User->scopes = ['senior' => ['User.id <' => '2']];
  78. $this->Comment->Behaviors->load('Tools.NamedScope');
  79. $this->Comment->User->Behaviors->load('Tools.NamedScope');
  80. $options = [
  81. 'contain' => ['User'],
  82. 'scope' => ['Comment.active', 'User.senior']
  83. ];
  84. $after = $this->Comment->find('count', $options);
  85. $this->assertSame(2, $after);
  86. }
  87. /**
  88. * NamedScopeBehaviorTest::testScopedFind()
  89. *
  90. * @return void
  91. */
  92. public function testScopedFind() {
  93. $this->Comment->scopes = ['active' => ['Comment.published' => 'Y']];
  94. $this->Comment->User->scopes = ['senior' => ['User.id <' => '2']];
  95. $this->Comment->Behaviors->load('Tools.NamedScope');
  96. $this->Comment->User->Behaviors->load('Tools.NamedScope');
  97. $this->Comment->scopedFinds = [
  98. 'activeAndSenior' => [
  99. 'name' => 'Active and Senior',
  100. 'find' => [
  101. 'virtualFields' => [
  102. //'fullname' => "CONCAT(User.id, '-', User.user)"
  103. ],
  104. 'options' => [
  105. 'scope' => ['Comment.active', 'User.senior'],
  106. 'contain' => ['User'],
  107. 'fields' => ['User.id', 'User.user'],
  108. 'order' => ['User.user' => 'ASC'],
  109. ],
  110. ]
  111. ]
  112. ];
  113. $result = $this->Comment->scopedFind('activeAndSenior');
  114. $this->assertSame(2, count($result));
  115. $result = $this->Comment->scopedFind('activeAndSenior', ['type' => 'count']);
  116. $this->assertSame(2, $result);
  117. }
  118. /**
  119. * NamedScopeBehaviorTest::testScopedFindWithVirtualFields()
  120. *
  121. * @return void
  122. */
  123. public function testScopedFindWithVirtualFields() {
  124. $this->db = ConnectionManager::getDataSource('test');
  125. $this->skipIf(!($this->db instanceof Mysql), 'The virtualFields test is only compatible with Mysql.');
  126. $this->Comment->scopes = ['active' => ['Comment.published' => 'Y']];
  127. $this->Comment->User->scopes = ['senior' => ['User.id <' => '2']];
  128. $this->Comment->Behaviors->load('Tools.NamedScope');
  129. $this->Comment->User->Behaviors->load('Tools.NamedScope');
  130. $this->Comment->scopedFinds = [
  131. 'activeAndSenior' => [
  132. 'name' => 'Active and Senior',
  133. 'find' => [
  134. 'virtualFields' => [
  135. 'fullname' => "CONCAT(User.id, '-', User.user)"
  136. ],
  137. 'options' => [
  138. 'scope' => ['Comment.active', 'User.senior'],
  139. 'contain' => ['User'],
  140. 'fields' => ['User.id', 'fullname'],
  141. 'order' => ['fullname' => 'ASC'],
  142. ],
  143. ]
  144. ]
  145. ];
  146. $result = $this->Comment->scopedFind('activeAndSenior');
  147. $this->assertSame(2, count($result));
  148. $scopedFinds = $this->Comment->scopedFinds();
  149. $this->assertSame(['activeAndSenior' => 'Active and Senior'], $scopedFinds);
  150. }
  151. /**
  152. * NamedScopeBehaviorTest::testScopedFindWithLimit()
  153. *
  154. * @return void
  155. */
  156. public function testScopedFindWithLimit() {
  157. $this->Comment->scopes = ['active' => ['Comment.published' => 'Y']];
  158. $this->Comment->User->scopes = ['senior' => ['User.id <' => '2']];
  159. $this->Comment->Behaviors->load('Tools.NamedScope');
  160. $this->Comment->User->Behaviors->load('Tools.NamedScope');
  161. $this->Comment->scopedFinds = [
  162. 'activeAndSenior' => [
  163. 'name' => 'Active and Senior',
  164. 'find' => [
  165. 'virtualFields' => [
  166. 'fullname' => "CONCAT(User.id, '-', User.user)"
  167. ],
  168. 'options' => [
  169. 'scope' => ['Comment.active', 'User.senior'],
  170. 'contain' => ['User'],
  171. 'fields' => ['User.id', 'fullname'],
  172. 'order' => ['fullname' => 'ASC'],
  173. ],
  174. ]
  175. ]
  176. ];
  177. $result = $this->Comment->scopedFind('activeAndSenior', ['options' => ['limit' => 1]]);
  178. $this->assertSame(1, count($result));
  179. }
  180. /**
  181. * NamedScopeBehaviorTest::testScopedFindOverwrite()
  182. *
  183. * @return void
  184. */
  185. public function testScopedFindOverwrite() {
  186. $this->Comment->scopes = ['active' => ['Comment.published' => 'Y']];
  187. $this->Comment->Behaviors->load('Tools.NamedScope');
  188. $this->Comment->User->Behaviors->load('Tools.NamedScope');
  189. $this->Comment->scopedFinds = [
  190. 'active' => [
  191. 'name' => 'Active Comentators',
  192. 'find' => [
  193. 'type' => 'all',
  194. 'virtualFields' => [
  195. 'fullname' => "CONCAT(User.id, '-', User.user)"
  196. ],
  197. 'options' => [
  198. 'scope' => ['Comment.active'],
  199. 'contain' => ['User'],
  200. 'fields' => ['User.id', 'fullname'],
  201. 'order' => ['fullname' => 'ASC'],
  202. 'limit' => 5
  203. ]
  204. ]
  205. ]
  206. ];
  207. $result = $this->Comment->scopedFind('active', ['options' => ['limit' => 2]]);
  208. $this->assertSame(2, count($result));
  209. $result = $this->Comment->scopedFind('active', ['type' => 'count']);
  210. $this->assertSame(5, $result);
  211. $result = $this->Comment->scopedFind('active', ['type' => 'first', 'options' => ['fields' => ['User.id', 'User.created'], 'order' => ['User.id' => 'DESC']]]);
  212. $expected = [
  213. 'User' => [
  214. 'id' => 4,
  215. 'created' => '2007-03-17 01:22:23'
  216. ]
  217. ];
  218. $this->assertEquals($expected, $result);
  219. }
  220. /**
  221. * NamedScopeBehaviorTest::testException()
  222. *
  223. * @expectedException RuntimeException
  224. * @return void
  225. */
  226. public function testException() {
  227. $this->Comment->scopedFind('foo');
  228. }
  229. }