NumericPaginatorTest.php 6.2 KB

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