feat: add package checksum validation
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace WPContentSync\Tests\Unit\Package;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use WPContentSync\Package\PackageChecksum;
|
||||
|
||||
class PackageChecksumTest extends TestCase {
|
||||
public function test_it_creates_stable_record_checksums(): void {
|
||||
$records = array(
|
||||
'posts' => array(
|
||||
array(
|
||||
'title' => 'Example',
|
||||
'id' => 123,
|
||||
),
|
||||
),
|
||||
'terms' => array(),
|
||||
'media' => array(),
|
||||
'custom_post_types' => array(),
|
||||
);
|
||||
|
||||
self::assertSame(
|
||||
PackageChecksum::records( $records ),
|
||||
PackageChecksum::records( $records )
|
||||
);
|
||||
self::assertStringStartsWith( 'sha256:', PackageChecksum::records( $records ) );
|
||||
}
|
||||
|
||||
public function test_it_canonicalizes_associative_key_order(): void {
|
||||
$records = array(
|
||||
'posts' => array(
|
||||
array(
|
||||
'title' => 'Example',
|
||||
'id' => 123,
|
||||
),
|
||||
),
|
||||
'terms' => array(),
|
||||
'media' => array(),
|
||||
'custom_post_types' => array(),
|
||||
);
|
||||
$reordered = array(
|
||||
'media' => array(),
|
||||
'custom_post_types' => array(),
|
||||
'terms' => array(),
|
||||
'posts' => array(
|
||||
array(
|
||||
'id' => 123,
|
||||
'title' => 'Example',
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
self::assertSame( PackageChecksum::records( $records ), PackageChecksum::records( $reordered ) );
|
||||
}
|
||||
|
||||
public function test_it_verifies_record_checksums(): void {
|
||||
$records = array(
|
||||
'posts' => array(
|
||||
array(
|
||||
'id' => 123,
|
||||
),
|
||||
),
|
||||
'terms' => array(),
|
||||
'media' => array(),
|
||||
'custom_post_types' => array(),
|
||||
);
|
||||
$checksum = PackageChecksum::records( $records );
|
||||
|
||||
self::assertTrue( PackageChecksum::verifyRecords( $records, $checksum ) );
|
||||
self::assertFalse( PackageChecksum::verifyRecords( $records, 'sha256:not-real' ) );
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace WPContentSync\Tests\Unit\Package;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use WPContentSync\Package\PackageChecksum;
|
||||
use WPContentSync\Package\PackageValidator;
|
||||
|
||||
class PackageValidatorTest extends TestCase {
|
||||
@@ -53,6 +54,16 @@ class PackageValidatorTest extends TestCase {
|
||||
self::assertSame( array( 'manifest.posts must match records.posts count.' ), $result->errors() );
|
||||
}
|
||||
|
||||
public function test_it_rejects_invalid_record_checksums(): void {
|
||||
$package = $this->validPackage();
|
||||
$package['checksums']['records'] = 'sha256:wrong';
|
||||
|
||||
$result = ( new PackageValidator() )->validate( $package );
|
||||
|
||||
self::assertFalse( $result->isValid() );
|
||||
self::assertSame( array( 'checksums.records does not match records payload.' ), $result->errors() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
@@ -86,7 +97,19 @@ class PackageValidatorTest extends TestCase {
|
||||
'custom_post_types' => array(),
|
||||
),
|
||||
'checksums' => array(
|
||||
'records' => 'sha256:abc123',
|
||||
'records' => PackageChecksum::records(
|
||||
array(
|
||||
'posts' => array(
|
||||
array(
|
||||
'id' => 123,
|
||||
'type' => 'post',
|
||||
),
|
||||
),
|
||||
'terms' => array(),
|
||||
'media' => array(),
|
||||
'custom_post_types' => array(),
|
||||
)
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user