Skip to content

Commit

Permalink
Make OAI record sorting consistent across paginated pages
Browse files Browse the repository at this point in the history
  • Loading branch information
lianna-blanca committed Apr 18, 2023
1 parent 93f1ab0 commit 1989ad3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Controllers/OaiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ protected function fetchOaiRecords(?string $from = null, ?string $until = null,
}

if (!$filters) {
return PaginatedList::create(OaiRecord::get());
return PaginatedList::create(OaiRecord::get()->sort('LastEdited ASC'));
}

$list = OaiRecord::get()
Expand Down
54 changes: 53 additions & 1 deletion tests/Controllers/OaiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

namespace Terraformers\OpenArchive\Tests\Controllers;

use ReflectionClass;
use SilverStripe\Dev\FunctionalTest;
use SimpleXMLElement;
use Terraformers\OpenArchive\Controllers\OaiController;
use Terraformers\OpenArchive\Documents\OaiDocument;
use Terraformers\OpenArchive\Models\OaiRecord;

class OaiControllerTest extends FunctionalTest
{

protected $usesDatabase = true; // phpcs:ignore
protected static $fixture_file = 'OaiControllerTest.yml';

public function testXmlResponseHeader(): void
{
Expand Down Expand Up @@ -120,4 +123,53 @@ public function verbUrlProvider(): array
];
}

public function testFetchOaiRecords(): void
{
// Reflect the OaiController so we can access the protected function fetchOaiRecords()
$controller = OaiController::create();
$reflection = new ReflectionClass(OaiController::class);
$method = $reflection->getMethod('fetchOaiRecords');
$method->setAccessible(true);

/** @var DOMElement $result */
$result = $method->invoke($controller);
$this->assertNotNull($result);

// Create variables to access each YML file record
$recordA = $this->objFromFixture(OaiRecord::class, 'recordA');
$recordB = $this->objFromFixture(OaiRecord::class, 'recordB');
$recordC = $this->objFromFixture(OaiRecord::class, 'recordC');

// Check that the first and last items in the list match
// their order in the YML file
$this->assertEquals('Random Record A', $result->first()->Titles);
$this->assertEquals('Random Record C', $result->last()->Titles);

// Update the records with new titles and check that the LastEdited date has changed for each
$recordAInitialEditDate = $recordA->LastEdited;
$recordBInitialEditDate = $recordB->LastEdited;
$recordCInitialEditDate = $recordC->LastEdited;

sleep(1); // Pause is required so the items do not all update in the same millisecond
$recordB->Titles = 'First Edited';
$recordB->write();
$this->assertNotEquals($recordBInitialEditDate, $recordB->LastEdited);

sleep(1);
$recordC->Titles = 'Second Edited';
$recordC->write();
$this->assertNotEquals($recordCInitialEditDate, $recordC->LastEdited);

sleep(1);
$recordA->Titles = 'Third Edited';
$recordA->write();
$this->assertNotEquals($recordAInitialEditDate, $recordA->LastEdited);

// Update the result
$result = $method->invoke($controller);

// Check that the first and last items in the list now match the order in which they were edited
$this->assertEquals('First Edited', $result->first()->Titles);
$this->assertEquals('Third Edited', $result->last()->Titles);
}
}
14 changes: 14 additions & 0 deletions tests/Controllers/OaiControllerTest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Terraformers\OpenArchive\Models\OaiSet:
set1:
Title: Set1

Terraformers\OpenArchive\Models\OaiRecord:
recordA:
Titles: Random Record A
OaiSets: =>Terraformers\OpenArchive\Models\OaiSet.set1
recordB:
Titles: Random Record B
OaiSets: =>Terraformers\OpenArchive\Models\OaiSet.set1
recordC:
Titles: Random Record C
OaiSets: =>Terraformers\OpenArchive\Models\OaiSet.set1

0 comments on commit 1989ad3

Please sign in to comment.