PaginatorTest.php 6.6 KB

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