1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Mtf\Troubleshooting;
use Magento\Mtf\ObjectManagerInterface;
use Magento\Mtf\Troubleshooting\Helper\UrlAnalyzer;
use Magento\Mtf\Util\Protocol\CurlTransport;
use Magento\Mtf\Util\Protocol\CurlTransport\BackendDecorator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Analyze Magento Admin.
*/
class AdminAnalyzer extends \Symfony\Component\Console\Command\Command
{
/**
* Console output of formatted messages.
*
* @var \Magento\Mtf\Console\Output
*/
private $output;
/**
* Object manager instance.
*
* @var ObjectManagerInterface
*/
private $objectManager;
/**
* Url analyzer helper.
*
* @var UrlAnalyzer
*/
private $urlAnalyzer;
/**
* @param ObjectManagerInterface $objectManager
* @param UrlAnalyzer $urlAnalyzer
*/
public function __construct(
ObjectManagerInterface $objectManager,
UrlAnalyzer $urlAnalyzer
) {
parent::__construct();
$this->objectManager = $objectManager;
$this->urlAnalyzer = $urlAnalyzer;
}
/**
* Configure command.
*
* @return void
*/
protected function configure()
{
parent::configure();
$this->setName('troubleshooting:check-magento-admin')
->setDescription('Check that app_backend_url is correct and admin can log in to Admin.');
}
/**
* Execute command.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
\PHPUnit\Util\Configuration::getInstance(MTF_PHPUNIT_FILE)->handlePHPConfiguration();
$this->output = $this->objectManager->create(
\Magento\Mtf\Console\Output::class,
['output' => $output]
);
$this->output->writeln("Verifying Magento Admin...");
$adminUrlAnalyzerMessages = $this->runAdminUrlAnalyzer();
if (isset($adminUrlAnalyzerMessages['error']) === false) {
$this->output->outputMessages($this->urlAnalyzer->checkDomain($_ENV['app_backend_url']));
} else {
$this->output->outputMessages($adminUrlAnalyzerMessages);
}
$this->output->writeln("Admin verification finished.");
}
/**
* Execute Admin url analyzer check.
*
* @return null|array
*/
public function runAdminUrlAnalyzer()
{
if (!isset($_ENV['app_backend_url'])) {
$messages['error'][] = 'app_backend_url parameter is absent in the phpunit.xml file. '
. 'Please, copy parameter from phpunit.xml.dist.';
return $messages;
}
$this->output->outputMessages($this->urlAnalyzer->fixLastSlash('app_backend_url'));
$url1 = $_ENV['app_backend_url'];
if (strpos($url1, '/index.php') !== false) {
$url2 = str_replace('/index.php', '', $url1);
} else {
$pattern = '/(\/\w+\/)$/';
$replacement = '/index.php$1';
$url2 = str_replace($url1, preg_replace($pattern, $replacement, $url1), $url1);
}
$urls = [$url1, $url2];
$isUrlValid = false;
foreach ($urls as $url) {
$_ENV['app_backend_url'] = $url;
try {
$config = \Magento\Mtf\ObjectManagerFactory::getObjectManager()->create(
\Magento\Mtf\Config\DataInterface::class
);
$curl = new BackendDecorator(new CurlTransport(), $config);
$response = $curl->read();
if (strpos($response, '404') !== false) {
break;
}
$curl->close();
$isUrlValid = true;
break;
} catch (\Exception $e) {
continue;
}
}
if ($isUrlValid == false) {
$messages['error'][] = 'Check correctness of app_backend_url in phpunit.xml.';
return $messages;
} elseif ($url1 != $_ENV['app_backend_url']) {
return $this->urlAnalyzer->resolveIndexPhpProblem($_ENV['app_backend_url']);
}
}
}