Verified Commit ab75cee9 authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3384725 by acbramley, smustgrave: Add pagination to VersionHistoryController

(cherry picked from commit 9048b09b)
parent d0015fde
Loading
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -25,6 +25,8 @@
 */
class VersionHistoryController extends ControllerBase {

  const REVISIONS_PER_PAGE = 50;

  /**
   * Constructs a new VersionHistoryController.
   *
@@ -213,6 +215,7 @@ protected function loadRevisions(RevisionableInterface $entity) {
      ->allRevisions()
      ->condition($entityType->getKey('id'), $entity->id())
      ->sort($entityType->getKey('revision'), 'DESC')
      ->pager(self::REVISIONS_PER_PAGE)
      ->execute();

    $currentLangcode = $this->languageManager
@@ -249,6 +252,8 @@ protected function revisionOverview(RevisionableInterface $entity): array {
      $build['entity_revisions_table']['#rows'][$revision->getRevisionId()] = $this->buildRow($revision);
    }

    $build['pager'] = ['#type' => 'pager'];

    (new CacheableMetadata())
      // Only dealing with this entity and no external dependencies.
      ->addCacheableDependency($entity)
+41 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\FunctionalTests\Entity;

use Drupal\Core\Entity\Controller\VersionHistoryController;
use Drupal\entity_test\Entity\EntityTestRev;
use Drupal\entity_test_revlog\Entity\EntityTestWithRevisionLog;
use Drupal\Tests\BrowserTestBase;
@@ -323,4 +324,44 @@ public function testOperationDeleteRevision(): void {
    $this->assertSession()->elementExists('named', ['link', 'Delete'], $row1);
  }

  /**
   * Test revisions are paginated.
   */
  public function testRevisionsPagination(): void {
    /** @var \Drupal\entity_test\Entity\EntityTestRev $entity */
    $entity = EntityTestRev::create([
      'type' => 'entity_test_rev',
      'name' => 'view all revisions,view revision',
    ]);
    $entity->save();

    $firstRevisionId = $entity->getRevisionId();

    for ($i = 0; $i < VersionHistoryController::REVISIONS_PER_PAGE; $i++) {
      $entity->setNewRevision(TRUE);
      // We need to change something on the entity for it to be considered a new
      // revision to display. We need "view all revisions" and "view revision"
      // in a comma separated string to grant access.
      $entity->setName('view all revisions,view revision,' . $i)->save();
    }

    $this->drupalGet($entity->toUrl('version-history'));
    $this->assertSession()->elementsCount('css', 'table tbody tr', VersionHistoryController::REVISIONS_PER_PAGE);
    $this->assertSession()->elementExists('css', '.pager');

    /** @var \Drupal\Core\Entity\RevisionableStorageInterface $storage */
    $storage = $this->container->get('entity_type.manager')->getStorage($entity->getEntityTypeId());
    $firstRevision = $storage->loadRevision($firstRevisionId);
    $secondRevision = $storage->loadRevision($firstRevisionId + 1);
    // We should see everything up to the second revision, but not the first.
    $this->assertSession()->linkByHrefExists($secondRevision->toUrl('revision')->toString());
    $this->assertSession()->linkByHrefNotExists($firstRevision->toUrl('revision')->toString());
    // The next page should show just the first revision.
    $this->clickLink('Go to next page');
    $this->assertSession()->elementsCount('css', 'table tbody tr', 1);
    $this->assertSession()->elementExists('css', '.pager');
    $this->assertSession()->linkByHrefNotExists($secondRevision->toUrl('revision')->toString());
    $this->assertSession()->linkByHrefExists($firstRevision->toUrl('revision')->toString());
  }

}