Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make OAI record sorting consistent across paginated pages #19

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great unit test! Good use of Reflection 😄

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'; // phpcs:ignore

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