|
| 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 | +} |
0 commit comments