Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AdminExecutor.php
Go to the documentation of this file.
1 <?php
8 
12 
17 {
23  private $transport;
24 
30  private $formKey = null;
31 
37  private $response;
38 
43  private $removeBackend;
44 
50  private static $adminUrl;
51 
59  public function __construct($removeBackend)
60  {
61  if (!isset(parent::$baseUrl)) {
62  parent::resolveBaseUrl();
63  }
64  self::$adminUrl = parent::$baseUrl . getenv('MAGENTO_BACKEND_NAME') . '/';
65  $this->removeBackend = $removeBackend;
66  $this->transport = new CurlTransport();
67  $this->authorize();
68  }
69 
76  private function authorize()
77  {
78  // Perform GET to backend url so form_key is set
79  $this->transport->write(self::$adminUrl, [], CurlInterface::GET);
80  $this->read();
81 
82  // Authenticate admin user
83  $authUrl = self::$adminUrl . 'admin/auth/login/';
84  $data = [
85  'login[username]' => getenv('MAGENTO_ADMIN_USERNAME'),
86  'login[password]' => getenv('MAGENTO_ADMIN_PASSWORD'),
87  'form_key' => $this->formKey,
88  ];
89  $this->transport->write($authUrl, $data, CurlInterface::POST);
90  $response = $this->read();
91  if (strpos($response, 'login-form')) {
92  throw new TestFrameworkException('Admin user authentication failed!');
93  }
94  }
95 
101  private function setFormKey()
102  {
103  preg_match('!var FORM_KEY = \'(\w+)\';!', $this->response, $matches);
104  if (!empty($matches[1])) {
105  $this->formKey = $matches[1];
106  }
107  }
108 
119  public function write($url, $data = [], $method = CurlInterface::POST, $headers = [])
120  {
121  $url = ltrim($url, "/");
122  $apiUrl = self::$adminUrl . $url;
123 
124  if ($this->removeBackend) {
125  $apiUrl = parent::$baseUrl . $url;
126  }
127 
128  if ($this->formKey) {
129  $data['form_key'] = $this->formKey;
130  } else {
131  throw new TestFrameworkException(
132  sprintf('Form key is absent! Url: "%s" Response: "%s"', $apiUrl, $this->response)
133  );
134  }
135 
136  $this->transport->write($apiUrl, str_replace('null', '', http_build_query($data)), $method, $headers);
137  }
138 
147  public function read($successRegex = null, $returnRegex = null)
148  {
149  $this->response = $this->transport->read();
150  $this->setFormKey();
151 
152  if (!empty($successRegex)) {
153  preg_match($successRegex, $this->response, $successMatches);
154  if (empty($successMatches)) {
155  throw new TestFrameworkException("Entity creation was not successful! Response: $this->response");
156  }
157  }
158 
159  if (!empty($returnRegex)) {
160  preg_match($returnRegex, $this->response, $returnMatches);
161  if (!empty($returnMatches)) {
162  return $returnMatches;
163  }
164  }
165  return $this->response;
166  }
167 
175  public function addOption($option, $value)
176  {
177  $this->transport->addOption($option, $value);
178  }
179 
185  public function close()
186  {
187  $this->transport->close();
188  }
189 }
write($url, $data=[], $method=CurlInterface::POST, $headers=[])
$value
Definition: gender.phtml:16
$method
Definition: info.phtml:13