Skip to content

Commit bf34ada

Browse files
committed
MQE-377: Implement data persistence through frontend http request.
1 parent 9c2544b commit bf34ada

File tree

3 files changed

+222
-16
lines changed

3 files changed

+222
-16
lines changed

src/Magento/FunctionalTestingFramework/DataGenerator/Persist/Curl/AdminExecutor.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ public function __construct()
5959
}
6060

6161
/**
62-
* Authorize customer on backend.
62+
* Authorize admin on backend.
6363
*
6464
* @return void
6565
* @throws TestFrameworkException
6666
*/
67-
public function authorize()
67+
private function authorize()
6868
{
6969
// Perform GET to backend url so form_key is set
7070
$this->transport->write(self::$adminUrl, [], CurlInterface::GET);
@@ -85,7 +85,7 @@ public function authorize()
8585
}
8686

8787
/**
88-
* Init Form Key from response.
88+
* Set Form Key from response.
8989
*
9090
* @return void
9191
*/
@@ -114,7 +114,7 @@ public function write($url, $data = [], $method = CurlInterface::POST, $headers
114114
$data['form_key'] = $this->formKey;
115115
} else {
116116
throw new TestFrameworkException(
117-
sprintf('Form key is absent! Url: "%s" Response: "%s"', $url, $this->response)
117+
sprintf('Form key is absent! Url: "%s" Response: "%s"', $apiUrl, $this->response)
118118
);
119119
}
120120

@@ -135,22 +135,14 @@ public function read($successRegex = null, $returnRegex = null)
135135
$this->setFormKey();
136136

137137
if (!empty($successRegex)) {
138-
preg_match(
139-
'/' . preg_quote($successRegex, '/') . '/',
140-
$this->response,
141-
$successMatches
142-
);
138+
preg_match($successRegex, $this->response, $successMatches);
143139
if (empty($successMatches)) {
144140
throw new TestFrameworkException("Entity creation was not successful! Response: $this->response");
145141
}
146142
}
147143

148144
if (!empty($returnRegex)) {
149-
preg_match(
150-
'/' . preg_quote($returnRegex, '/') . '/',
151-
$this->response,
152-
$returnMatches
153-
);
145+
preg_match($returnRegex, $this->response, $returnMatches);
154146
if (!empty($returnMatches)) {
155147
return $returnMatches;
156148
}
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\FunctionalTestingFramework\DataGenerator\Persist\Curl;
8+
9+
use Magento\FunctionalTestingFramework\Util\Protocol\CurlInterface;
10+
use Magento\FunctionalTestingFramework\Util\Protocol\CurlTransport;
11+
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
12+
13+
/**
14+
* Curl executor for requests to Frontend.
15+
*/
16+
class FrontendExecutor extends AbstractExecutor implements CurlInterface
17+
{
18+
/**
19+
* Curl transport protocol.
20+
*
21+
* @var CurlTransport
22+
*/
23+
private $transport;
24+
25+
/**
26+
* Form key.
27+
*
28+
* @var string
29+
*/
30+
private $formKey = null;
31+
32+
/**
33+
* Response data.
34+
*
35+
* @var string
36+
*/
37+
private $response;
38+
39+
/**
40+
* Cookies data.
41+
*
42+
* @var string
43+
*/
44+
private $cookies = '';
45+
46+
/**
47+
* Customer email used for authentication.
48+
*
49+
* @var string
50+
*/
51+
private $customerEmail;
52+
53+
/**
54+
* Customer password used for authentication.
55+
*
56+
* @var string
57+
*/
58+
private $customerPassword;
59+
60+
/**
61+
* FrontendExecutor constructor.
62+
*
63+
* @param string $customerEmail
64+
* @param string $customerPassWord
65+
*/
66+
public function __construct($customerEmail, $customerPassWord)
67+
{
68+
if (!isset(parent::$baseUrl)) {
69+
parent::resolveBaseUrl();
70+
}
71+
$this->transport = new CurlTransport();
72+
$this->customerEmail = $customerEmail;
73+
$this->customerPassword = $customerPassWord;
74+
$this->authorize();
75+
}
76+
77+
/**
78+
* Authorize customer on frontend.
79+
*
80+
* @return void
81+
* @throws TestFrameworkException
82+
*/
83+
private function authorize()
84+
{
85+
$url = parent::$baseUrl . 'customer/account/login/';
86+
$this->transport->write($url);
87+
$this->read();
88+
89+
$url = parent::$baseUrl . 'customer/account/loginPost/';
90+
$data = [
91+
'login[username]' => $this->customerEmail,
92+
'login[password]' => $this->customerPassword,
93+
'form_key' => $this->formKey,
94+
];
95+
$this->transport->write($url, $data, CurlInterface::POST, ['Set-Cookie:' . $this->cookies]);
96+
$response = $this->read();
97+
if (strpos($response, 'customer/account/login')) {
98+
throw new TestFrameworkException($this->customerEmail . ', cannot be logged in by curl handler!');
99+
}
100+
}
101+
102+
/**
103+
* Set Form Key from response.
104+
*
105+
* @return void
106+
*/
107+
private function setFormKey()
108+
{
109+
$str = substr($this->response, strpos($this->response, 'form_key'));
110+
preg_match('/value="(.*)" \/>/', $str, $matches);
111+
if (!empty($matches[1])) {
112+
$this->formKey = $matches[1];
113+
}
114+
}
115+
116+
/**
117+
* Set Cookies from response.
118+
*
119+
* @return void
120+
*/
121+
protected function setCookies()
122+
{
123+
preg_match_all('|Set-Cookie: (.*);|U', $this->response, $matches);
124+
if (!empty($matches[1])) {
125+
$this->cookies = implode('; ', $matches[1]);
126+
}
127+
}
128+
129+
/**
130+
* Send request to the remote server.
131+
*
132+
* @param string $url
133+
* @param array $data
134+
* @param string $method
135+
* @param mixed $headers
136+
* @return void
137+
* @throws TestFrameworkException
138+
*/
139+
public function write($url, $data = [], $method = CurlInterface::POST, $headers = [])
140+
{
141+
if(isset($data['customer_email'])) {
142+
unset($data['customer_email']);
143+
}
144+
if(isset($data['customer_password'])) {
145+
unset($data['customer_password']);
146+
}
147+
$apiUrl = parent::$baseUrl . $url;
148+
if ($this->formKey) {
149+
$data['form_key'] = $this->formKey;
150+
} else {
151+
throw new TestFrameworkException(
152+
sprintf('Form key is absent! Url: "%s" Response: "%s"', $apiUrl, $this->response)
153+
);
154+
}
155+
$headers = ['Set-Cookie:' . $this->cookies];
156+
$this->transport->write($apiUrl, str_replace('null', '', http_build_query($data)), $method, $headers);
157+
}
158+
159+
/**
160+
* Read response from server.
161+
*
162+
* @param string $successRegex
163+
* @param string $returnRegex
164+
* @return string|array
165+
* @throws TestFrameworkException
166+
*/
167+
public function read($successRegex = null, $returnRegex = null)
168+
{
169+
$this->response = $this->transport->read();
170+
$this->setCookies();
171+
$this->setFormKey();
172+
173+
if (!empty($successRegex)) {
174+
preg_match($successRegex, $this->response, $successMatches);
175+
if (empty($successMatches)) {
176+
throw new TestFrameworkException("Entity creation was not successful! Response: $this->response");
177+
}
178+
}
179+
180+
if (!empty($returnRegex)) {
181+
preg_match($returnRegex, $this->response, $returnMatches);
182+
if (!empty($returnMatches)) {
183+
return $returnMatches;
184+
}
185+
}
186+
return $this->response;
187+
}
188+
189+
/**
190+
* Add additional option to cURL.
191+
*
192+
* @param int $option the CURLOPT_* constants
193+
* @param mixed $value
194+
* @return void
195+
*/
196+
public function addOption($option, $value)
197+
{
198+
$this->transport->addOption($option, $value);
199+
}
200+
201+
/**
202+
* Close the connection to the server.
203+
*
204+
* @return void
205+
*/
206+
public function close()
207+
{
208+
$this->transport->close();
209+
}
210+
}

src/Magento/FunctionalTestingFramework/DataGenerator/Persist/CurlHandler.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\FunctionalTestingFramework\DataGenerator\Persist;
77

88
use Magento\FunctionalTestingFramework\DataGenerator\Persist\Curl\AdminExecutor;
9+
use Magento\FunctionalTestingFramework\DataGenerator\Persist\Curl\FrontendExecutor;
910
use Magento\FunctionalTestingFramework\DataGenerator\Persist\Curl\WebapiExecutor;
1011
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\OperationDefinitionObjectHandler;
1112
use Magento\FunctionalTestingFramework\DataGenerator\Objects\EntityDataObject;
@@ -123,8 +124,11 @@ public function executeRequest($dependentEntities)
123124
$executor = new WebapiExecutor($this->storeCode);
124125
} elseif ($authorization === 'adminFormKey') {
125126
$executor = new AdminExecutor();
126-
//TODO: add frontend request executor
127-
//} elseif ($authorization === 'customFromKey') {
127+
} elseif ($authorization === 'customerFormKey') {
128+
$executor = new FrontendExecutor(
129+
$this->requestData['customer_email'],
130+
$this->requestData['customer_password']
131+
);
128132
}
129133

130134
if (!$executor) {

0 commit comments

Comments
 (0)