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' ) );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user