Skip to content

Commit

Permalink
Change EncryptionTest to test the encrypt and decrypt methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
vaurdan committed Jul 28, 2021
1 parent c9f684e commit 1f6b87c
Showing 1 changed file with 17 additions and 25 deletions.
42 changes: 17 additions & 25 deletions tests/test-encryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,39 +32,31 @@ public function setUp() {
}

/**
* Test new instance of Syndication_Encryption with the MCrypt strategy.
*
* @requires extension mcrypt
* Test if the `encrypt` method on Syndication_Encryption calls the `encrypt` method on the specific Syndication_Encryptor
*/
public function test_new_encryption_instance_with_mcrypt() {
$syndication_encryption = new \Syndication_Encryption( new \Syndication_Encryptor_MCrypt() );
public function test_encrypt_method_is_called_on_encryptor_object() {
$fake_encrypted_string = 'I\'m an encrypted string.';

// Disable deprecated warnings for PHP 7..
$original_error_reporting = error_reporting( error_reporting() & ~E_DEPRECATED ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.runtime_configuration_error_reporting
$mock_encryptor = $this->createMock( \Syndication_Encryptor::class );
$mock_encryptor->method( 'encrypt' )->will( $this->returnValue( $fake_encrypted_string ) );

// Quick encryption/decryption test.
$quick_test_string = 'This is a quick test.';
$encrypted = $syndication_encryption->encrypt( $quick_test_string );
$decrypted = $syndication_encryption->decrypt( $encrypted );
self::assertEquals( $encrypted, $syndication_encryption->encrypt( $quick_test_string ), 'assert that encryption results are consistent' );
self::assertEquals( $quick_test_string, $decrypted, 'assert that decrypted string is the same as original' );
$syndication_encryption = new \Syndication_Encryption( $mock_encryptor );

// Restore original error reporting.
error_reporting( $original_error_reporting ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.runtime_configuration_error_reporting
self::assertSame( $fake_encrypted_string, $syndication_encryption->encrypt( 'I am a plain-text string' ) );
}

/**
* Test new instance of Syndication_Encryption with the OpenSSL strategy.
* Test if the `decrypt` method on Syndication_Encryption calls the `decrypt` method on the specific Syndication_Encryptor
*/
public function test_new_encryption_instance_with_openssl() {
$syndication_encryption = new \Syndication_Encryption( new \Syndication_Encryptor_OpenSSL() );

// Quick encryption/decryption test.
$quick_test_string = 'This is a quick test.';
$encrypted = $syndication_encryption->encrypt( $quick_test_string );
$decrypted = $syndication_encryption->decrypt( $encrypted );
self::assertEquals( $encrypted, $syndication_encryption->encrypt( $quick_test_string ), 'assert that encryption results are consistent' );
self::assertEquals( $quick_test_string, $decrypted, 'assert that decrypted string is the same as original' );
public function test_decrypt_method_is_called_on_encryptor_object() {
$fake_plain_text_string = 'I am a plain-text string.';

$mock_encryptor = $this->createMock( \Syndication_Encryptor::class );
$mock_encryptor->method( 'decrypt' )->will( $this->returnValue( $fake_plain_text_string ) );

$syndication_encryption = new \Syndication_Encryption( $mock_encryptor );

self::assertSame( $fake_plain_text_string, $syndication_encryption->decrypt( 'I\'m an encrypted string.' ) );
}

/**
Expand Down

0 comments on commit 1f6b87c

Please sign in to comment.