Replace `global` keyword with `$GLOBALS`
[phpmyadmin.git] / test / classes / Crypto / CryptoTest.php
blobf136004e29dbd9bfb1840032be82ef9c7ba06d6f
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin\Tests\Crypto;
7 use PhpMyAdmin\Crypto\Crypto;
8 use PhpMyAdmin\Tests\AbstractTestCase;
10 use function mb_strlen;
11 use function str_repeat;
13 /**
14 * @covers \PhpMyAdmin\Crypto\Crypto
16 class CryptoTest extends AbstractTestCase
18 public function testWithValidKeyFromConfig(): void
20 $_SESSION = [];
21 $GLOBALS['config']->set('URLQueryEncryptionSecretKey', str_repeat('a', 32));
23 $crypto = new Crypto();
24 $encrypted = $crypto->encrypt('test');
25 $this->assertNotSame('test', $encrypted);
26 $this->assertSame('test', $crypto->decrypt($encrypted));
27 $this->assertArrayNotHasKey('URLQueryEncryptionSecretKey', $_SESSION);
30 public function testWithValidKeyFromSession(): void
32 $_SESSION = ['URLQueryEncryptionSecretKey' => str_repeat('a', 32)];
33 $GLOBALS['config']->set('URLQueryEncryptionSecretKey', '');
35 $crypto = new Crypto();
36 $encrypted = $crypto->encrypt('test');
37 $this->assertNotSame('test', $encrypted);
38 $this->assertSame('test', $crypto->decrypt($encrypted));
39 $this->assertArrayHasKey('URLQueryEncryptionSecretKey', $_SESSION);
42 public function testWithNewSessionKey(): void
44 $_SESSION = [];
45 $GLOBALS['config']->set('URLQueryEncryptionSecretKey', '');
47 $crypto = new Crypto();
48 $encrypted = $crypto->encrypt('test');
49 $this->assertNotSame('test', $encrypted);
50 $this->assertSame('test', $crypto->decrypt($encrypted));
51 $this->assertArrayHasKey('URLQueryEncryptionSecretKey', $_SESSION);
52 $this->assertEquals(32, mb_strlen($_SESSION['URLQueryEncryptionSecretKey'], '8bit'));
55 public function testDecryptWithInvalidKey(): void
57 $_SESSION = [];
58 $GLOBALS['config']->set('URLQueryEncryptionSecretKey', str_repeat('a', 32));
60 $crypto = new Crypto();
61 $encrypted = $crypto->encrypt('test');
62 $this->assertNotSame('test', $encrypted);
63 $this->assertSame('test', $crypto->decrypt($encrypted));
65 $GLOBALS['config']->set('URLQueryEncryptionSecretKey', str_repeat('b', 32));
67 $crypto = new Crypto();
68 $this->assertNull($crypto->decrypt($encrypted));