Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CryptographerTest.php
Go to the documentation of this file.
1 <?php
7 
11 use Magento\Analytics\Model\EncodedContextFactory;
13 
14 class CryptographerTest extends \PHPUnit\Framework\TestCase
15 {
19  private $analyticsTokenMock;
20 
24  private $encodedContextFactoryMock;
25 
29  private $encodedContextMock;
30 
34  private $objectManagerHelper;
35 
39  private $cryptographer;
40 
44  private $key;
45 
49  private $initializationVectors;
50 
54  private $source;
55 
59  private $cipherMethod = 'AES-256-CBC';
60 
64  protected function setUp()
65  {
66  $this->analyticsTokenMock = $this->getMockBuilder(AnalyticsToken::class)
67  ->disableOriginalConstructor()
68  ->getMock();
69 
70  $this->encodedContextFactoryMock = $this->getMockBuilder(EncodedContextFactory::class)
71  ->setMethods(['create'])
72  ->disableOriginalConstructor()
73  ->getMock();
74 
75  $this->encodedContextMock = $this->getMockBuilder(EncodedContext::class)
76  ->disableOriginalConstructor()
77  ->getMock();
78 
79  $this->key = '';
80  $this->source = '';
81  $this->initializationVectors = [];
82 
83  $this->objectManagerHelper = new ObjectManagerHelper($this);
84 
85  $this->cryptographer = $this->objectManagerHelper->getObject(
86  Cryptographer::class,
87  [
88  'analyticsToken' => $this->analyticsTokenMock,
89  'encodedContextFactory' => $this->encodedContextFactoryMock,
90  'cipherMethod' => $this->cipherMethod,
91  ]
92  );
93  }
94 
98  public function testEncode()
99  {
100  $token = 'some-token-value';
101  $this->source = 'Some text';
102  $this->key = hash('sha256', $token);
103 
104  $checkEncodedContext = function ($parameters) {
105  $emptyRequiredParameters =
106  array_diff(['content', 'initializationVector'], array_keys(array_filter($parameters)));
107  if ($emptyRequiredParameters) {
108  return false;
109  }
110 
111  $encryptedData = openssl_encrypt(
112  $this->source,
113  $this->cipherMethod,
114  $this->key,
115  OPENSSL_RAW_DATA,
116  $parameters['initializationVector']
117  );
118 
119  return ($encryptedData === $parameters['content']);
120  };
121 
122  $this->analyticsTokenMock
123  ->expects($this->once())
124  ->method('getToken')
125  ->with()
126  ->willReturn($token);
127 
128  $this->encodedContextFactoryMock
129  ->expects($this->once())
130  ->method('create')
131  ->with($this->callback($checkEncodedContext))
132  ->willReturn($this->encodedContextMock);
133 
134  $this->assertSame($this->encodedContextMock, $this->cryptographer->encode($this->source));
135  }
136 
141  {
142  $this->source = 'Some text';
143  $token = 'some-token-value';
144 
145  $registerInitializationVector = function ($parameters) {
146  if (empty($parameters['initializationVector'])) {
147  return false;
148  }
149 
150  $this->initializationVectors[] = $parameters['initializationVector'];
151 
152  return true;
153  };
154 
155  $this->analyticsTokenMock
156  ->expects($this->exactly(2))
157  ->method('getToken')
158  ->with()
159  ->willReturn($token);
160 
161  $this->encodedContextFactoryMock
162  ->expects($this->exactly(2))
163  ->method('create')
164  ->with($this->callback($registerInitializationVector))
165  ->willReturn($this->encodedContextMock);
166 
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));
170  }
171 
176  public function testEncodeNotValidSource($source)
177  {
178  $this->cryptographer->encode($source);
179  }
180 
185  {
186  return [
187  'Array' => [[]],
188  'Empty string' => [''],
189  ];
190  }
191 
196  {
197  $source = 'Some string';
198  $cryptographer = $this->objectManagerHelper->getObject(
199  Cryptographer::class,
200  [
201  'cipherMethod' => 'Wrong-method',
202  ]
203  );
204 
205  $cryptographer->encode($source);
206  }
207 
211  public function testEncodeTokenNotValid()
212  {
213  $source = 'Some string';
214 
215  $this->analyticsTokenMock
216  ->expects($this->once())
217  ->method('getToken')
218  ->with()
219  ->willReturn(null);
220 
221  $this->cryptographer->encode($source);
222  }
223 }