|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\FunctionalTestingFramework\Upgrade; |
| 8 | + |
| 9 | +use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig; |
| 10 | +use Magento\FunctionalTestingFramework\Page\Handlers\PageObjectHandler; |
| 11 | +use Magento\FunctionalTestingFramework\Test\Handlers\TestObjectHandler; |
| 12 | +use Magento\FunctionalTestingFramework\Util\ModuleResolver; |
| 13 | +use Symfony\Component\Console\Input\InputInterface; |
| 14 | +use Symfony\Component\Filesystem\Filesystem; |
| 15 | +use Symfony\Component\Finder\Finder; |
| 16 | + |
| 17 | +/** |
| 18 | + * Class UpgradePageWithNoUrl |
| 19 | + * @package Magento\FunctionalTestingFramework\Upgrade |
| 20 | + */ |
| 21 | +class UpdatePageWithNoUrl implements UpgradeInterface |
| 22 | +{ |
| 23 | + /** |
| 24 | + * Upgrades all test xml files, replacing {{page}} with {{page.url}} |
| 25 | + * |
| 26 | + * @param InputInterface $input |
| 27 | + * @return string |
| 28 | + */ |
| 29 | + public function execute(InputInterface $input) |
| 30 | + { |
| 31 | + $testsPath = $input->getArgument('path'); |
| 32 | + $finder = new Finder(); |
| 33 | + $finder->files()->in($testsPath)->name("*.xml"); |
| 34 | + |
| 35 | + // Safely ensure we can get test materials to check for existing pages later |
| 36 | + try { |
| 37 | + MftfApplicationConfig::create(); |
| 38 | + ModuleResolver::getInstance()->getModulesPath(); |
| 39 | + } catch (\Exception $e) { |
| 40 | + //retry with --force |
| 41 | + MftfApplicationConfig::create(true); |
| 42 | + ModuleResolver::getInstance()->getModulesPath(); |
| 43 | + } |
| 44 | + |
| 45 | + $fileSystem = new Filesystem(); |
| 46 | + $testsUpdated = 0; |
| 47 | + foreach ($finder->files() as $file) { |
| 48 | + $count = 0; |
| 49 | + $contents = $file->getContents(); |
| 50 | + // Find {{page}} but not {{page.url}} |
| 51 | + preg_match_all("/{{([^\.}]*)}}/", $contents, $pageReferences); |
| 52 | + if (empty($pageReferences[1])) { |
| 53 | + continue; |
| 54 | + } |
| 55 | + foreach ($pageReferences[1] as $index => $potentialReplace) { |
| 56 | + $isPage = PageObjectHandler::getInstance()->getObject($potentialReplace); |
| 57 | + if ($isPage === null) { |
| 58 | + continue; |
| 59 | + } |
| 60 | + $contents = str_replace($pageReferences[0][$index], "{{" . $potentialReplace . ".url}}", $contents); |
| 61 | + $count++; |
| 62 | + } |
| 63 | + $fileSystem->dumpFile($file->getRealPath(), $contents); |
| 64 | + if ($count > 0) { |
| 65 | + $testsUpdated++; |
| 66 | + } |
| 67 | + } |
| 68 | + return ("{{Page}} without specified \".url\" updated in {$testsUpdated} file(s)."); |
| 69 | + } |
| 70 | +} |
0 commit comments