11 use Magento\Analytics\Model\EncodedContextFactory;
19 private $analyticsTokenMock;
24 private $encodedContextFactoryMock;
29 private $encodedContextMock;
34 private $objectManagerHelper;
39 private $cryptographer;
49 private $initializationVectors;
59 private $cipherMethod =
'AES-256-CBC';
66 $this->analyticsTokenMock = $this->getMockBuilder(AnalyticsToken::class)
67 ->disableOriginalConstructor()
70 $this->encodedContextFactoryMock = $this->getMockBuilder(EncodedContextFactory::class)
71 ->setMethods([
'create'])
72 ->disableOriginalConstructor()
75 $this->encodedContextMock = $this->getMockBuilder(EncodedContext::class)
76 ->disableOriginalConstructor()
81 $this->initializationVectors = [];
83 $this->objectManagerHelper =
new ObjectManagerHelper($this);
85 $this->cryptographer = $this->objectManagerHelper->getObject(
88 'analyticsToken' => $this->analyticsTokenMock,
89 'encodedContextFactory' => $this->encodedContextFactoryMock,
90 'cipherMethod' => $this->cipherMethod,
100 $token =
'some-token-value';
101 $this->source =
'Some text';
102 $this->key = hash(
'sha256',
$token);
104 $checkEncodedContext =
function ($parameters) {
105 $emptyRequiredParameters =
106 array_diff([
'content',
'initializationVector'], array_keys(array_filter($parameters)));
107 if ($emptyRequiredParameters) {
111 $encryptedData = openssl_encrypt(
116 $parameters[
'initializationVector']
119 return ($encryptedData === $parameters[
'content']);
122 $this->analyticsTokenMock
123 ->expects($this->once())
128 $this->encodedContextFactoryMock
129 ->expects($this->once())
131 ->with($this->callback($checkEncodedContext))
132 ->willReturn($this->encodedContextMock);
134 $this->assertSame($this->encodedContextMock, $this->cryptographer->encode($this->source));
142 $this->source =
'Some text';
143 $token =
'some-token-value';
145 $registerInitializationVector =
function ($parameters) {
146 if (empty($parameters[
'initializationVector'])) {
150 $this->initializationVectors[] = $parameters[
'initializationVector'];
155 $this->analyticsTokenMock
156 ->expects($this->exactly(2))
161 $this->encodedContextFactoryMock
162 ->expects($this->exactly(2))
164 ->with($this->callback($registerInitializationVector))
165 ->willReturn($this->encodedContextMock);
167 $this->assertSame($this->encodedContextMock, $this->cryptographer->encode($this->source));
168 $this->assertSame($this->encodedContextMock, $this->cryptographer->encode($this->source));
169 $this->assertCount(2, array_unique($this->initializationVectors));
178 $this->cryptographer->encode($source);
188 'Empty string' => [
''],
197 $source =
'Some string';
198 $cryptographer = $this->objectManagerHelper->getObject(
199 Cryptographer::class,
201 'cipherMethod' =>
'Wrong-method',
205 $cryptographer->encode($source);
213 $source =
'Some string';
215 $this->analyticsTokenMock
216 ->expects($this->once())
221 $this->cryptographer->encode($source);
testEncodeUniqueInitializationVector()
testEncodeTokenNotValid()
testEncodeNotValidSource($source)
testEncodeNotValidCipherMethod()
encodeNotValidSourceDataProvider()