SimplePaginatorTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.9.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Datasource;
  16. use Cake\Core\Configure;
  17. use Cake\Datasource\SimplePaginator;
  18. use Cake\ORM\Entity;
  19. class SimplePaginatorTest extends PaginatorTest
  20. {
  21. /**
  22. * fixtures property
  23. *
  24. * @var array
  25. */
  26. public $fixtures = [
  27. 'core.Posts', 'core.Articles', 'core.ArticlesTags',
  28. 'core.Authors', 'core.AuthorsTags', 'core.Tags'
  29. ];
  30. /**
  31. * Don't load data for fixtures for all tests
  32. *
  33. * @var bool
  34. */
  35. public $autoFixtures = false;
  36. public function setUp(): void
  37. {
  38. parent::setUp();
  39. Configure::write('App.namespace', 'TestApp');
  40. $this->Paginator = new SimplePaginator();
  41. $this->Post = $this->getMockRepository();
  42. }
  43. /**
  44. * test paginate() and custom find, to make sure the correct count is returned.
  45. *
  46. * @return void
  47. */
  48. public function testPaginateCustomFind()
  49. {
  50. $this->loadFixtures('Posts');
  51. $titleExtractor = function ($result) {
  52. $ids = [];
  53. foreach ($result as $record) {
  54. $ids[] = $record->title;
  55. }
  56. return $ids;
  57. };
  58. $table = $this->getTableLocator()->get('PaginatorPosts');
  59. $data = ['author_id' => 3, 'title' => 'Fourth Post', 'body' => 'Article Body, unpublished', 'published' => 'N'];
  60. $result = $table->save(new Entity($data));
  61. $this->assertNotEmpty($result);
  62. $result = $this->Paginator->paginate($table);
  63. $this->assertCount(4, $result, '4 rows should come back');
  64. $this->assertEquals(['First Post', 'Second Post', 'Third Post', 'Fourth Post'], $titleExtractor($result));
  65. $pagingParams = $this->Paginator->getPagingParams();
  66. $this->assertEquals(4, $pagingParams['PaginatorPosts']['current']);
  67. $this->assertNull($pagingParams['PaginatorPosts']['count']);
  68. $settings = ['finder' => 'published'];
  69. $result = $this->Paginator->paginate($table, [], $settings);
  70. $this->assertCount(3, $result, '3 rows should come back');
  71. $this->assertEquals(['First Post', 'Second Post', 'Third Post'], $titleExtractor($result));
  72. $pagingParams = $this->Paginator->getPagingParams();
  73. $this->assertEquals(3, $pagingParams['PaginatorPosts']['current']);
  74. $this->assertNull($pagingParams['PaginatorPosts']['count']);
  75. $settings = ['finder' => 'published', 'limit' => 2, 'page' => 2];
  76. $result = $this->Paginator->paginate($table, [], $settings);
  77. $this->assertCount(1, $result, '1 rows should come back');
  78. $this->assertEquals(['Third Post'], $titleExtractor($result));
  79. $pagingParams = $this->Paginator->getPagingParams();
  80. $this->assertEquals(1, $pagingParams['PaginatorPosts']['current']);
  81. $this->assertNull($pagingParams['PaginatorPosts']['count']);
  82. $this->assertSame(0, $pagingParams['PaginatorPosts']['pageCount']);
  83. $settings = ['finder' => 'published', 'limit' => 2];
  84. $result = $this->Paginator->paginate($table, [], $settings);
  85. $this->assertCount(2, $result, '2 rows should come back');
  86. $this->assertEquals(['First Post', 'Second Post'], $titleExtractor($result));
  87. $pagingParams = $this->Paginator->getPagingParams();
  88. $this->assertEquals(2, $pagingParams['PaginatorPosts']['current']);
  89. $this->assertNull($pagingParams['PaginatorPosts']['count']);
  90. $this->assertEquals(0, $pagingParams['PaginatorPosts']['pageCount']);
  91. $this->assertTrue($pagingParams['PaginatorPosts']['nextPage']);
  92. $this->assertFalse($pagingParams['PaginatorPosts']['prevPage']);
  93. $this->assertEquals(2, $pagingParams['PaginatorPosts']['perPage']);
  94. $this->assertNull($pagingParams['PaginatorPosts']['limit']);
  95. }
  96. /**
  97. * test paginate() and custom find with fields array, to make sure the correct count is returned.
  98. *
  99. * @return void
  100. */
  101. public function testPaginateCustomFindFieldsArray()
  102. {
  103. $this->loadFixtures('Posts');
  104. $table = $this->getTableLocator()->get('PaginatorPosts');
  105. $data = ['author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N'];
  106. $table->save(new Entity($data));
  107. $settings = [
  108. 'finder' => 'list',
  109. 'conditions' => ['PaginatorPosts.published' => 'Y'],
  110. 'limit' => 2
  111. ];
  112. $results = $this->Paginator->paginate($table, [], $settings);
  113. $result = $results->toArray();
  114. $expected = [
  115. 1 => 'First Post',
  116. 2 => 'Second Post',
  117. ];
  118. $this->assertEquals($expected, $result);
  119. $result = $this->Paginator->getPagingParams()['PaginatorPosts'];
  120. $this->assertEquals(2, $result['current']);
  121. $this->assertNull($result['count']);
  122. $this->assertEquals(0, $result['pageCount']);
  123. $this->assertTrue($result['nextPage']);
  124. $this->assertFalse($result['prevPage']);
  125. }
  126. /**
  127. * Test that special paginate types are called and that the type param doesn't leak out into defaults or options.
  128. *
  129. * @return void
  130. */
  131. public function testPaginateCustomFinder()
  132. {
  133. $settings = [
  134. 'PaginatorPosts' => [
  135. 'finder' => 'published',
  136. 'fields' => ['id', 'title'],
  137. 'maxLimit' => 10,
  138. ]
  139. ];
  140. $this->loadFixtures('Posts');
  141. $table = $this->getTableLocator()->get('PaginatorPosts');
  142. $table->updateAll(['published' => 'N'], ['id' => 2]);
  143. $this->Paginator->paginate($table, [], $settings);
  144. $pagingParams = $this->Paginator->getPagingParams();
  145. $this->assertSame('published', $pagingParams['PaginatorPosts']['finder']);
  146. $this->assertSame(1, $pagingParams['PaginatorPosts']['start']);
  147. $this->assertSame(2, $pagingParams['PaginatorPosts']['end']);
  148. // nextPage will be always true for SimplePaginator
  149. $this->assertTrue($pagingParams['PaginatorPosts']['nextPage']);
  150. }
  151. }