Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/MAGETWO-51929-setup-wizard-from-…
Browse files Browse the repository at this point in the history
…pub' into PR_Branch
  • Loading branch information
ark99 committed Jun 8, 2016
2 parents 430ce9c + 5d8e3f3 commit 4c7e58c
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 0 deletions.
47 changes: 47 additions & 0 deletions app/code/Magento/Backend/Model/Setup/MenuBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Backend\Model\Setup;

use Magento\Backend\Model\Menu;
use Magento\Backend\Model\Menu\Builder;
use Magento\Framework\App\DocRootLocator;

/**
* Plugin class to remove web setup wizard from menu if application root is pub/ and no setup url variable is specified.
*/
class MenuBuilder
{
/**
* @var DocRootLocator
*/
protected $docRootLocator;

/**
* MenuBuilder constructor.
*
* @param DocRootLocator $docRootLocator
*/
public function __construct(DocRootLocator $docRootLocator)
{
$this->docRootLocator = $docRootLocator;
}

/**
* Removes 'Web Setup Wizard' from the menu if doc root is pub and no setup url variable is specified.
*
* @param Builder $subject
* @param Menu $menu
* @return Menu
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterGetResult(Builder $subject, Menu $menu)
{
if ($this->docRootLocator->isPub()) {
$menu->remove('Magento_Backend::setup_wizard');
}
return $menu;
}
}
42 changes: 42 additions & 0 deletions app/code/Magento/Backend/Test/Unit/Model/MenuBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Backend\Test\Unit\Model;

use Magento\Backend\Model\Setup\MenuBuilder;

class MenuBuilderTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider afterGetResultDataProvider
*
* @param string $isPub
* @param int $times
* @param bool $result
*/
public function testAfterGetResult($isPub, $times)
{
$docRootLocator = $this->getMock('\Magento\Framework\App\DocRootLocator', [], [], '', false);
$docRootLocator->expects($this->once())->method('isPub')->willReturn($isPub);
$model = new MenuBuilder($docRootLocator);
/** @var \Magento\Backend\Model\Menu $menu */
$menu = $this->getMock('\Magento\Backend\Model\Menu', [], [], '', false);
$menu->expects($this->exactly($times))->method('remove')->willReturn(true);

/** @var \Magento\Backend\Model\Menu\Builder $menuBuilder */
$menuBuilder = $this->getMock('\Magento\Backend\Model\Menu\Builder', [], [], '', false);

$this->assertInstanceOf(
'\Magento\Backend\Model\Menu',
$model->afterGetResult($menuBuilder, $menu)
);
}

public function afterGetResultDataProvider()
{
return [[true, 1], [false, 0],];
}
}
3 changes: 3 additions & 0 deletions app/code/Magento/Backend/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,7 @@
</argument>
</arguments>
</type>
<type name="Magento\Backend\Model\Menu\Builder">
<plugin name="SetupMenuBuilder" type="Magento\Backend\Model\Setup\MenuBuilder" />
</type>
</config>
48 changes: 48 additions & 0 deletions lib/internal/Magento/Framework/App/DocRootLocator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Framework\App;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem\Directory\ReadFactory;

/**
* This class calculates if document root is set to pub
*/
class DocRootLocator
{
/**
* @var RequestInterface
*/
private $request;

/**
* @var ReadFactory
*/
private $readFactory;

/**
* @param RequestInterface $request
* @param ReadFactory $readFactory
*/
public function __construct(RequestInterface $request, ReadFactory $readFactory)
{
$this->request = $request;
$this->readFactory = $readFactory;
}

/**
* Returns true if doc root is pub/ and not BP
*
* @return bool
*/
public function isPub()
{
$rootBasePath = $this->request->getServer('DOCUMENT_ROOT');
$readDirectory = $this->readFactory->create(DirectoryList::ROOT);
return strpos($rootBasePath, 'pub') && !$readDirectory->isExist($rootBasePath . 'setup');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Framework\App\Test\Unit;

use Magento\Framework\App\DocRootLocator;

class DocRootLocatorTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider isPubDataProvider
*
* @param string $path
* @param bool $isExist
* @param bool $result
*/
public function testIsPub($path, $isExist, $result)
{
$request = $this->getMock('\Magento\Framework\App\Request\Http', [], [], '', false);
$request->expects($this->once())->method('getServer')->willReturn($path);
$reader = $this->getMock('\Magento\Framework\Filesystem\Directory\Read', [], [], '', false);
$reader->expects($this->any())->method('isExist')->willReturn($isExist);
$readFactory = $this->getMock('\Magento\Framework\Filesystem\Directory\ReadFactory', [], [], '', false);
$readFactory->expects($this->once())->method('create')->willReturn($reader);
$model = new DocRootLocator($request, $readFactory);
$this->assertSame($result, $model->isPub());
}

public function isPubDataProvider()
{
return [
['/some/path/to/root', false, false],
['/some/path/to/root', true, false],
['/some/path/to/pub', false, true],
['/some/path/to/pub', true, false],
];
}
}

0 comments on commit 4c7e58c

Please sign in to comment.