Skip to content

Commit

Permalink
Add manual tests and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
convenient committed Nov 30, 2018
1 parent 28d2949 commit d76a35e
Show file tree
Hide file tree
Showing 27 changed files with 277 additions and 13 deletions.
4 changes: 4 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### Checklist
- [ ] Pull request has a meaningful description of its purpose
- [ ] All commits are accompanied by meaningful commit messages
- [ ] Tests have been ran / updated
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/.idea
/vendor/
dev/instances/magento*
dev/output/actual/*
45 changes: 37 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,27 @@ Helper scripts to aid upgrading magento 2 websites
This tool checks for
- Preferences
- Overrides
- phtml / js
- layout xml

## ⚠️ Warning ⚠️

This tool is experimental and a work in progress. It will not catch every preference/override/etc.

If you have any improvements please raise a PR or an Issue.

Look for todo's in the code base.

## How to use

All the below should be used on a local setup, never do any of this anywhere near a production website.

### Step 1 - Update the Magento core and dependencies then generate a diff
### Step 1 - Update the Magento core with dependencies then generate a patch

In your project `composer install` and move the original vendor directory to a backup location

```
cd /path/to/magento2/
composer install
mv vendor/ vendor_orig/
```
Expand All @@ -36,24 +47,42 @@ Once you have a completed the composer steps you can create a diff which can be
diff -ur vendor_orig/ vendor/ > vendor.patch
```

### Step 2 - Parse the diff file
### Step 2 - Parse the patch file

In a clone of this repository you can analyze the project and patch file.
In a clone of this repository you can analyse the project and patch file.


```php
git clone https://github.com/AmpersandHQ/ampersand-magento2-upgrade-patch-helper
cd ampersand-magento2-upgrade-patch-helper
composer install
php bin/patch-helper.php analyse /path/to/magento2/
```

This will output a grid like follows
This will output a grid of files which have overrides/preferences that need to be reviewed and possibly updated to match the patch file.

```
TODO
+---------------------------------------------------------------------------------------+---------------------------------------------+
| Core file | Preference |
+---------------------------------------------------------------------------------------+---------------------------------------------+
| vendor/magento/module-advanced-pricing-import-export/Model/Export/AdvancedPricing.php | Ampersand\Test\Model\Export\AdvancedPricing |
+---------------------------------------------------------------------------------------+---------------------------------------------+
+-------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------+
| Core file | Override (phtml/js) |
+-------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------+
| vendor/magento/module-customer/view/frontend/templates/account/dashboard/info.phtml | app/design/frontend/Ampersand/theme/Magento_Customer/templates/account/dashboard/info.phtml |
| vendor/magento/module-customer/view/frontend/web/js/model/authentication-popup.js | app/design/frontend/Ampersand/theme/Magento_Customer/web/js/model/authentication-popup.js |
+-------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------+
+------------------------------------------------------------------------+--------------------------------------------------------------------------------+
| Core file | Override/extended (layout xml) |
+------------------------------------------------------------------------+--------------------------------------------------------------------------------+
| vendor/magento/module-sales/view/frontend/layout/sales_order_print.xml | app/design/frontend/Ampersand/theme/Magento_Sales/layout/sales_order_print.xml |
+------------------------------------------------------------------------+--------------------------------------------------------------------------------+
```

## Warnings

This tool is experimental and a work in progress. It may not catch every preference/override/etc.
## Tests

Have a look in the `./dev/Makefile` to see how the tests work.

Any problems raise a PR or an Issue.
They do not run automatically in travis as you cannot run `composer create-project --repository-url=https://repo.magento.com/` without providing and exposing an access token to the world.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
}
],
"require": {
"symfony/console": "^3.4"
"symfony/console": "^3.4",
"php": "~7.1"
},
"autoload": {
"classmap": ["src/"],
Expand Down
16 changes: 16 additions & 0 deletions dev/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
list: # Lists all available commands, the default command when running `make` with no arguments
grep -v -e "^\t" Makefile | grep . | awk -F":.+?#" '{ print $$1 " #" $$2 }' | column -t -s '#'

configure-magento-2-1: # Install magento 2.1 with the test module, and create vendor.patch for analysis
./setup-magento-with-diff.sh 2.1.13 2.1.16 21

test-magento-2-1: # Analyse 2.1 with vendor.patch
php ../bin/patch-helper.php analyse $(shell pwd)/instances/magento21 > output/actual/magento21.out.txt
diff output/expected/magento21.out.txt output/actual/magento21.out.txt

configure-magento-2-2: # Install magento 2.2 with the test module, and create vendor.patch for analysis
./setup-magento-with-diff.sh 2.2.5 2.2.6 22

test-magento-2-2: # Analyse 2.2 with vendor.patch
php ../bin/patch-helper.php analyse $(shell pwd)/instances/magento22 > output/actual/magento22.out.txt
diff output/expected/magento22.out.txt output/actual/magento22.out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace Ampersand\Test\Model\Export;

class Directpost extends \Magento\Authorizenet\Model\Directpost
{
//todo detect this
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace Ampersand\Test\Model\Admin\Export;

class AdvancedPricing extends \Magento\AdvancedPricingImportExport\Model\Export\AdvancedPricing
{
//todo detect this
}
7 changes: 7 additions & 0 deletions dev/TestModule/app/code/Ampersand/Test/Model/Directpost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace Ampersand\Test\Model;

class Directpost extends \Magento\Authorizenet\Model\Directpost
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace Ampersand\Test\Model\Export;

class AdvancedPricing extends \Magento\AdvancedPricingImportExport\Model\Export\AdvancedPricing
{

}
56 changes: 56 additions & 0 deletions dev/TestModule/app/code/Ampersand/Test/Setup/UpgradeData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Ampersand\Test\Setup;

use Magento\Framework\App\Cache;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Theme\Model\Theme\ThemeProvider;
use Magento\Theme\Model\Config as ThemeConfig;
use Magento\Theme\Model\ThemeFactory as ThemeFactory;

class UpgradeData implements UpgradeDataInterface
{
/** @var \Magento\Theme\Model\Theme\ThemeProvider */
private $themeProvider;
/** @var \Magento\Theme\Model\Config */
private $themeConfig;
/** @var \Magento\Theme\Model\ThemeFactory */
private $themeFactory;
/** @var \Magento\Framework\App\Cache */
private $cache;

/**
* @param \Magento\Theme\Model\Theme\ThemeProvider $themeProvider
* @param \Magento\Theme\Model\Config $themeConfig
* @param \Magento\Framework\App\Cache $cache
*/
public function __construct(
ThemeProvider $themeProvider,
ThemeConfig $themeConfig,
ThemeFactory $themeFactory,
Cache $cache
) {
$this->themeProvider = $themeProvider;
$this->themeConfig = $themeConfig;
$this->themeFactory = $themeFactory;
$this->cache = $cache;
}

/**
* Upgrades data for a module
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
*/
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
if (version_compare($context->getVersion(), '1.0.0', '<')) {
/** @var \Magento\Theme\Model\Theme $theme */
$theme = $this->themeProvider->getThemeByFullPath('frontend/Ampersand/theme');
$this->themeConfig->assignToStore($theme, [1]);
$this->cache->clean();
}
}
}
5 changes: 5 additions & 0 deletions dev/TestModule/app/code/Ampersand/Test/etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\AdvancedPricingImportExport\Model\Export\AdvancedPricing" type="Ampersand\Test\Model\Admin\Export\AdvancedPricing" />
<preference for="Magento\Authorizenet\Model\Directpost" type="Ampersand\Test\Model\Admin\Directpost" />
</config>
5 changes: 5 additions & 0 deletions dev/TestModule/app/code/Ampersand/Test/etc/frontend/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\AdvancedPricingImportExport\Model\Export\AdvancedPricing" type="Ampersand\Test\Model\Export\AdvancedPricing" />
<preference for="Magento\Authorizenet\Model\Directpost" type="Ampersand\Test\Model\Directpost" />
</config>
10 changes: 10 additions & 0 deletions dev/TestModule/app/code/Ampersand/Test/etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Ampersand_Test" active="true" setup_version="1.0.0">
<sequence>
<module name="Magento_AdvancedPricing"/>
<module name="Magento_Theme"/>
</sequence>
</module>
</config>
7 changes: 7 additions & 0 deletions dev/TestModule/app/code/Ampersand/Test/registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
use Magento\Framework\Component\ComponentRegistrar;

$registrar = new ComponentRegistrar();
if ($registrar->getPath(ComponentRegistrar::MODULE, 'Ampersand_Test') === null) {
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Ampersand_Test', __DIR__);
}
10 changes: 10 additions & 0 deletions dev/TestModule/app/design/frontend/Ampersand/theme/etc/view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<view xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/view.xsd">

</view>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::THEME,
'frontend/Ampersand/theme',
__DIR__
);


5 changes: 5 additions & 0 deletions dev/TestModule/app/design/frontend/Ampersand/theme/theme.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0"?>
<theme xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/theme.xsd">
<title>Ampersand Theme</title>
<parent>Magento/luma</parent>
</theme>
Empty file added dev/instances/.gitkeep
Empty file.
Empty file added dev/output/actual/.gitkeep
Empty file.
16 changes: 16 additions & 0 deletions dev/output/expected/magento21.out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
+---------------------------------------------------------+---------------------------------+
| Core file | Preference |
+---------------------------------------------------------+---------------------------------+
| vendor/magento/module-authorizenet/Model/Directpost.php | Ampersand\Test\Model\Directpost |
+---------------------------------------------------------+---------------------------------+
+-------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------+
| Core file | Override (phtml/js) |
+-------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------+
| vendor/magento/module-customer/view/frontend/templates/account/dashboard/info.phtml | app/design/frontend/Ampersand/theme/Magento_Customer/templates/account/dashboard/info.phtml |
| vendor/magento/module-customer/view/frontend/web/js/model/authentication-popup.js | app/design/frontend/Ampersand/theme/Magento_Customer/web/js/model/authentication-popup.js |
+-------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------+
+------------------------------------------------------------------------+--------------------------------------------------------------------------------+
| Core file | Override/extended (layout xml) |
+------------------------------------------------------------------------+--------------------------------------------------------------------------------+
| vendor/magento/module-sales/view/frontend/layout/sales_order_print.xml | app/design/frontend/Ampersand/theme/Magento_Sales/layout/sales_order_print.xml |
+------------------------------------------------------------------------+--------------------------------------------------------------------------------+
17 changes: 17 additions & 0 deletions dev/output/expected/magento22.out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
+---------------------------------------------------------------------------------------+---------------------------------------------+
| Core file | Preference |
+---------------------------------------------------------------------------------------+---------------------------------------------+
| vendor/magento/module-advanced-pricing-import-export/Model/Export/AdvancedPricing.php | Ampersand\Test\Model\Export\AdvancedPricing |
| vendor/magento/module-authorizenet/Model/Directpost.php | Ampersand\Test\Model\Directpost |
+---------------------------------------------------------------------------------------+---------------------------------------------+
+-------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------+
| Core file | Override (phtml/js) |
+-------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------+
| vendor/magento/module-customer/view/frontend/templates/account/dashboard/info.phtml | app/design/frontend/Ampersand/theme/Magento_Customer/templates/account/dashboard/info.phtml |
| vendor/magento/module-customer/view/frontend/web/js/model/authentication-popup.js | app/design/frontend/Ampersand/theme/Magento_Customer/web/js/model/authentication-popup.js |
+-------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------+
+------------------------------------------------------------------------+--------------------------------------------------------------------------------+
| Core file | Override/extended (layout xml) |
+------------------------------------------------------------------------+--------------------------------------------------------------------------------+
| vendor/magento/module-sales/view/frontend/layout/sales_order_print.xml | app/design/frontend/Ampersand/theme/Magento_Sales/layout/sales_order_print.xml |
+------------------------------------------------------------------------+--------------------------------------------------------------------------------+
43 changes: 43 additions & 0 deletions dev/setup-magento-with-diff.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env bash
set -e

FROM=$1
TO=$2
ID=$3

#rm -rf ./instances/magento$ID
#mysql -hlocalhost -uroot -e "drop database testmagento$ID;"
mysql -hlocalhost -uroot -e "create database testmagento$ID;"

composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=$FROM ./instances/magento$ID/
cd instances/magento$ID/
composer install

# Backup vendor and upgrade magento
mv vendor/ vendor_orig/
composer install
composer require magento/product-community-edition $TO --no-update
composer update composer/composer magento/product-community-edition --with-dependencies
composer install

# Install test module and theme
cd -
cp -r TestModule/app/code ./instances/magento$ID/app/
cp -r TestModule/app/design/frontend/Ampersand ./instances/magento$ID/app/design/frontend/
cd -

# Install magento
php -d memory_limit=1024M bin/magento setup:install \
--admin-firstname=ampersand --admin-lastname=developer [email protected] \
--admin-user=admin --admin-password=somepass123 \
--db-name=testmagento$ID --db-user=root --db-host=127.0.0.1\
--backend-frontname=admin \
--base-url=https://magento-$ID-develop.localhost/ \
--language=en_GB --currency=GBP --timezone=Europe/London \
--use-rewrites=1;

# Generate patch file for analysis
diff -ur vendor_orig/ vendor/ > vendor.patch || true

cd -
set +e
4 changes: 2 additions & 2 deletions src/Ampersand/PatchHelper/Command/AnalyseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected function configure()
$this
->setName('analyse')
->addArgument('project', InputArgument::REQUIRED, 'The path to the magento2 project')
->setDescription('Analyse a magento2 project which has had a ./vendor.diff file manually created');
->setDescription('Analyse a magento2 project which has had a ./vendor.patch file manually created');
}

protected function execute(InputInterface $input, OutputInterface $output)
Expand Down Expand Up @@ -47,7 +47,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$templateOverrideTable->setHeaders(['Core file', 'Override (phtml/js)']);

$layoutOverrideTable = new Table($output);
$layoutOverrideTable->setHeaders(['Core file', 'Override (layout xml)']);
$layoutOverrideTable->setHeaders(['Core file', 'Override/extended (layout xml)']);

foreach ($patchFile->getFiles() as $file) {
if (!$patchOverrideValidator->canValidate($file)) {
Expand Down
5 changes: 3 additions & 2 deletions src/Ampersand/PatchHelper/Helper/PatchOverrideValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function canValidate($file)
return false;
}
if (str_contains($file, '/ui_component/')) {
return false; //todo should these be checked?
return false; //todo could these be checked?
}
if (str_contains($file, '/Test/')) {
return false;
Expand Down Expand Up @@ -144,6 +144,7 @@ private function validatePhpFile($file)
$class = preg_replace('/\\.[^.\\s]{3,4}$/', '', $class);
$class = str_replace('/', '\\', $class);

//todo detect scoped(adminhtml) preferences as well as frontend preferences
$preference = $this->config->getPreference($class);

if ($preference === $class || $preference === "$class\\Interceptor") {
Expand Down Expand Up @@ -181,7 +182,7 @@ private function validateFrontendFile($file, $type)
$path = $this->minificationResolver->resolve($type, $name, $area, $this->currentTheme, null, $module);

if (!is_file($path)) {
throw new LogicException("Could not resolve $file (attempted to resolve to $path)");
throw new \LogicException("Could not resolve $file (attempted to resolve to $path)");
}
if ($path && strpos($path, '/vendor/magento/') === false) {
throw new FileOverrideException($path);
Expand Down

0 comments on commit d76a35e

Please sign in to comment.