PaginatorTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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
  26. */
  27. protected $fixtures = [
  28. 'core.Posts', 'core.Articles', 'core.ArticlesTags',
  29. 'core.Authors', 'core.AuthorsTags', 'core.Tags',
  30. ];
  31. /**
  32. * Don't load data for fixtures for all tests
  33. *
  34. * @var bool
  35. */
  36. public $autoFixtures = false;
  37. /**
  38. * test paginate() and custom find, to make sure the correct count is returned.
  39. *
  40. * @return void
  41. */
  42. public function testPaginateCustomFind()
  43. {
  44. $this->loadFixtures('Posts');
  45. $titleExtractor = function ($result) {
  46. $ids = [];
  47. foreach ($result as $record) {
  48. $ids[] = $record->title;
  49. }
  50. return $ids;
  51. };
  52. $table = $this->getTableLocator()->get('PaginatorPosts');
  53. $data = ['author_id' => 3, 'title' => 'Fourth Post', 'body' => 'Article Body, unpublished', 'published' => 'N'];
  54. $result = $table->save(new Entity($data));
  55. $this->assertNotEmpty($result);
  56. $result = $this->Paginator->paginate($table);
  57. $this->assertCount(4, $result, '4 rows should come back');
  58. $this->assertEquals(['First Post', 'Second Post', 'Third Post', 'Fourth Post'], $titleExtractor($result));
  59. $pagingParams = $this->Paginator->getPagingParams();
  60. $this->assertEquals(4, $pagingParams['PaginatorPosts']['current']);
  61. $this->assertEquals(4, $pagingParams['PaginatorPosts']['count']);
  62. $settings = ['finder' => 'published'];
  63. $result = $this->Paginator->paginate($table, [], $settings);
  64. $this->assertCount(3, $result, '3 rows should come back');
  65. $this->assertEquals(['First Post', 'Second Post', 'Third Post'], $titleExtractor($result));
  66. $pagingParams = $this->Paginator->getPagingParams();
  67. $this->assertEquals(3, $pagingParams['PaginatorPosts']['current']);
  68. $this->assertEquals(3, $pagingParams['PaginatorPosts']['count']);
  69. $settings = ['finder' => 'published', 'limit' => 2, 'page' => 2];
  70. $result = $this->Paginator->paginate($table, [], $settings);
  71. $this->assertCount(1, $result, '1 rows should come back');
  72. $this->assertEquals(['Third Post'], $titleExtractor($result));
  73. $pagingParams = $this->Paginator->getPagingParams();
  74. $this->assertEquals(1, $pagingParams['PaginatorPosts']['current']);
  75. $this->assertEquals(3, $pagingParams['PaginatorPosts']['count']);
  76. $this->assertEquals(2, $pagingParams['PaginatorPosts']['pageCount']);
  77. $settings = ['finder' => 'published', 'limit' => 2];
  78. $result = $this->Paginator->paginate($table, [], $settings);
  79. $this->assertCount(2, $result, '2 rows should come back');
  80. $this->assertEquals(['First Post', 'Second Post'], $titleExtractor($result));
  81. $pagingParams = $this->Paginator->getPagingParams();
  82. $this->assertEquals(2, $pagingParams['PaginatorPosts']['current']);
  83. $this->assertEquals(3, $pagingParams['PaginatorPosts']['count']);
  84. $this->assertEquals(2, $pagingParams['PaginatorPosts']['pageCount']);
  85. $this->assertTrue($pagingParams['PaginatorPosts']['nextPage']);
  86. $this->assertFalse($pagingParams['PaginatorPosts']['prevPage']);
  87. $this->assertEquals(2, $pagingParams['PaginatorPosts']['perPage']);
  88. $this->assertNull($pagingParams['PaginatorPosts']['limit']);
  89. }
  90. /**
  91. * test paginate() and custom find with fields array, to make sure the correct count is returned.
  92. *
  93. * @return void
  94. */
  95. public function testPaginateCustomFindFieldsArray()
  96. {
  97. $this->loadFixtures('Posts');
  98. $table = $this->getTableLocator()->get('PaginatorPosts');
  99. $data = ['author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N'];
  100. $table->save(new Entity($data));
  101. $settings = [
  102. 'finder' => 'list',
  103. 'conditions' => ['PaginatorPosts.published' => 'Y'],
  104. 'limit' => 2,
  105. ];
  106. $results = $this->Paginator->paginate($table, [], $settings);
  107. $result = $results->toArray();
  108. $expected = [
  109. 1 => 'First Post',
  110. 2 => 'Second Post',
  111. ];
  112. $this->assertEquals($expected, $result);
  113. $result = $this->Paginator->getPagingParams()['PaginatorPosts'];
  114. $this->assertEquals(2, $result['current']);
  115. $this->assertEquals(3, $result['count']);
  116. $this->assertEquals(2, $result['pageCount']);
  117. $this->assertTrue($result['nextPage']);
  118. $this->assertFalse($result['prevPage']);
  119. }
  120. /**
  121. * Test that special paginate types are called and that the type param doesn't leak out into defaults or options.
  122. *
  123. * @return void
  124. */
  125. public function testPaginateCustomFinder()
  126. {
  127. $settings = [
  128. 'PaginatorPosts' => [
  129. 'finder' => 'published',
  130. 'fields' => ['id', 'title'],
  131. 'maxLimit' => 10,
  132. ],
  133. ];
  134. $this->loadFixtures('Posts');
  135. $table = $this->getTableLocator()->get('PaginatorPosts');
  136. $table->updateAll(['published' => 'N'], ['id' => 2]);
  137. $this->Paginator->paginate($table, [], $settings);
  138. $pagingParams = $this->Paginator->getPagingParams();
  139. $this->assertSame('published', $pagingParams['PaginatorPosts']['finder']);
  140. $this->assertSame(1, $pagingParams['PaginatorPosts']['start']);
  141. $this->assertSame(2, $pagingParams['PaginatorPosts']['end']);
  142. $this->assertFalse($pagingParams['PaginatorPosts']['nextPage']);
  143. }
  144. }