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