Skip to content

Commit

Permalink
Updates for Silverstripe 5.x
Browse files Browse the repository at this point in the history
Includes Unit test changes for PHPUnit 9.6
  • Loading branch information
scott-nz committed Oct 30, 2023
1 parent a44b789 commit 2839e51
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This module allows Silverstripe CMS ORM data to be encrypted before being stored


## Requirements
* SilverStripe CMS 4.9
* SilverStripe CMS 5.0

## Installation
Install via Composer:
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
"source": "https://github.com/madmatt/silverstripe-encrypt-at-rest"
},
"require": {
"silverstripe/framework": "^4.9.0",
"silverstripe/framework": "^5",
"defuse/php-encryption": "^2.2"
},
"require-dev": {
"phpunit/phpunit": "^5.7"
"phpunit/phpunit": "^9.6"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion src/AtRestCryptoService.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ protected function getFullPath($file, $visibility = AssetStore::VISIBILITY_PROTE
$fileID = rtrim($filename, '\\/');
}

return $adapter->applyPathPrefix($fileID);
return $adapter->prefixPath($fileID);
}

}
3 changes: 1 addition & 2 deletions src/Extension/DecryptDataObjectFieldsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ class DecryptDataObjectFieldsExtension extends DataExtension
* if so, we decrypt these and inject them into the object during hydration so that the rest of the application only
* has to deal with the decrypted values everywhere.
*
* @param $record
* @return array
*/
public function augmentHydrateFields($record)
public function augmentHydrateFields()
{
// Look at $this->owner to determine if it has any encrypted database fields
$schema = DataObject::getSchema();
Expand Down
27 changes: 14 additions & 13 deletions tests/AtRestCryptoServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function testEncryptFile($filename, $contents, $visibility)
$file->publishFile();
}

$oldFilename = $adapter->applyPathPrefix(
$oldFilename = $adapter->prefixPath(
$strategy->buildFileID(
new ParsedFileID(
$file->getFilename(),
Expand All @@ -102,9 +102,9 @@ public function testEncryptFile($filename, $contents, $visibility)
$this->assertEquals($originalFilename, $file->getFilename());

if ($visibility === AssetStore::VISIBILITY_PROTECTED) {
$this->assertContains('assets/.protected/', $oldFilename);
$this->assertStringContainsString('assets/.protected/', $oldFilename);
} elseif ($visibility === AssetStore::VISIBILITY_PUBLIC) {
$this->assertNotContains('assets/.protected/', $oldFilename);
$this->assertStringNotContainsString('assets/.protected/', $oldFilename);
}

/** @var AtRestCryptoService $service */
Expand All @@ -114,9 +114,9 @@ public function testEncryptFile($filename, $contents, $visibility)
$this->assertEquals($originalFilename . '.enc', $encryptedFile->getFilename());

// Confirm the old file has been deleted
$this->assertFileNotExists($oldFilename);
$this->assertFileDoesNotExist($oldFilename);

$encryptedFilename = $adapter->applyPathPrefix(
$encryptedFilename = $adapter->prefixPath(
$strategy->buildFileID(
new ParsedFileID(
$encryptedFile->getFilename(),
Expand All @@ -130,20 +130,21 @@ public function testEncryptFile($filename, $contents, $visibility)
$this->assertFileExists($encryptedFilename);

if ($visibility === AssetStore::VISIBILITY_PROTECTED) {
$this->assertContains('assets/.protected/', $encryptedFilename);
$this->assertStringContainsString('assets/.protected/', $encryptedFilename);
} elseif ($visibility === AssetStore::VISIBILITY_PUBLIC) {
$this->assertNotContains('assets/.protected/', $encryptedFilename);
$this->assertStringNotContainsString('assets/.protected/', $encryptedFilename);
}

$encryptedFileString = $encryptedFile->getString() ?: '';
// Confirm the new file is encrypted
$this->assertFalse(ctype_print($encryptedFile->getString()));
$this->assertNotEquals($originalText, $encryptedFile->getString());
$this->assertFalse(ctype_print($encryptedFileString));
$this->assertNotEquals($originalText, $encryptedFileString);
$this->assertEquals($originalFilename, $encryptedFile->Name);
$this->assertEquals($originalFilename . '.enc', $file->getFilename());

// Now decrypt the file back
$decryptedFile = $service->decryptFile($encryptedFile, null, $visibility);
$decryptedFilename = $adapter->applyPathPrefix(
$decryptedFilename = $adapter->prefixPath(
$strategy->buildFileID(
new ParsedFileID(
$decryptedFile->getFilename(),
Expand All @@ -160,16 +161,16 @@ public function testEncryptFile($filename, $contents, $visibility)
$this->assertEquals($originalFilename, $decryptedFile->getFilename());

if ($visibility === AssetStore::VISIBILITY_PROTECTED) {
$this->assertContains('assets/.protected/', $decryptedFilename);
$this->assertStringContainsString('assets/.protected/', $decryptedFilename);
} elseif ($visibility === AssetStore::VISIBILITY_PUBLIC) {
$this->assertNotContains('assets/.protected/', $decryptedFilename);
$this->assertStringNotContainsString('assets/.protected/', $decryptedFilename);
}

// Confirm that original text has been decoded properly
$this->assertEquals($originalText, $decryptedFile->getString());

// Confirm that encrypted file has been deleted
$this->assertFileNotExists($encryptedFilename);
$this->assertFileDoesNotExist($encryptedFilename);
}

/**
Expand Down

0 comments on commit 2839e51

Please sign in to comment.