Skip to content

Commit

Permalink
Create the basics of an app:move command
Browse files Browse the repository at this point in the history
#29807 documents that moving apps
from the core to a non-core apps folder manually can cause issues. Given
that and the discussion in
owncloud-archive/documentation#4276, I decided to start
writing an app:move command, to make moving apps from the core apps
directory, to a custom non-core apps directory simpler and less messy.

When finished, the command will be able to:

- List apps that can be moved
- List any non-core app directories
- Check that non-core app directories exist
- Move one or more apps from the core apps directory to a non-core apps directory

The other reason for doing this is to better familiarise myself with
ownCloud's internals — something that, for quite some time, I've felt
I've been lacking, and that has been hindering me from doing my work
better.
  • Loading branch information
settermjd committed Aug 17, 2018
1 parent 58ebad0 commit 4fe6705
Show file tree
Hide file tree
Showing 2 changed files with 205 additions and 0 deletions.
93 changes: 93 additions & 0 deletions core/Command/App/MoveApps.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* @author Matthew Setter <matthew@matthewsetter.com>
*
* @copyright Copyright (c) 2018, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OC\Core\Command\App;

use OC\Core\Command\Base;
use OCP\App\IAppManager;
use OCP\IConfig;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class MoveApps extends Base {

/** @var IAppManager */
protected $manager;

/** * @var IConfig */
private $config;

/**
* @param IAppManager $manager
* @param IConfig $config
*/
public function __construct(IAppManager $manager, IConfig $config) {
parent::__construct();

$this->manager = $manager;
$this->config = $config;
}

protected function configure() {
parent::configure();

$this
->setName('app:move')
->setDescription('Move apps from the core apps directory to a non-core apps directory.')
->addArgument(
'listNonCoreDirectories',
InputArgument::OPTIONAL,
'List any non-core app directories'
)
;
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int|null|void
*/
protected function execute(InputInterface $input, OutputInterface $output) {
if ($input->hasArgument('listNonCoreDirectories')) {
$this->writeNonAppDirectories($input, $output);
}
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @param array $items
*/
protected function writeNonAppDirectories(InputInterface $input, OutputInterface $output) {
$appPaths = $this->config->getSystemValue('apps_paths');
$output->writeln('Non-App Directories:');
$directories = [];
foreach ($appPaths as $path) {
if ($path['url'] !== '/apps' && $path['writable'] === true) {
$directories[] = sprintf("%s -> %s", $path['url'], $path['path']);
}
}

parent::writeArrayInOutputFormat($input, $output, $directories);
}
}
112 changes: 112 additions & 0 deletions tests/Core/Command/Apps/AppsMoveTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
/**
* @author Matthew Setter <matthew@matthewsetter.com>
*
* @copyright Copyright (c) 2018, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace Tests\Core\Command\Config;

use OC\Core\Command\App\MoveApps;
use OCP\IConfig;
use Symfony\Component\Console\Tester\CommandTester;
use Test\TestCase;

/**
* Class AppsMoveTest
*
* This command can move apps from the core directory to a non-core location.
* The motivation for this is https://github.com/owncloud/documentation/issues/3621,
* which shows that errors can be created when moving apps by hand.
*
* This app will:
* - List apps that can be moved
* - List any non-core app directories
* - Check that non-core app directories exist
* - Move one or more apps from the core apps directory to a non-core apps directory
*/
class AppsMoveTest extends TestCase {

/** @var \PHPUnit_Framework_MockObject_MockObject|IConfig */
protected $config;

/** @var CommandTester */
private $commandTester;

public function setUp() {
parent::setUp();

$this->config = $this->config = $this->getMockBuilder(IConfig::class)
->disableOriginalConstructor()
->getMock();

$command = new MoveApps(\OC::$server->getAppManager(), $this->config);
$this->commandTester = new CommandTester($command);
}

/**
* @dataProvider providesAppIds
* @param array $input
* @param string $expectedOutput
*/
public function testCommandInput($input, $expectedOutput) {

$this->config->expects($this->atLeastOnce())
->method('getSystemValue')
->with('apps_paths')
->willReturn(
[
[
'path' => '/opt/core/apps',
'url' => '/apps',
'writable' => true,
],
[
'path' => '/opt/core/apps-external',
'url' => '/apps-external',
'writable' => true,
],
[
'path' => '/opt/core/apps-external-2',
'url' => '/apps-external-2',
'writable' => true,
],
[
'path' => '/opt/core/apps-external-not-writable',
'url' => '/apps-external-not-writable',
'writable' => false,
],
]
);

$this->commandTester->execute($input);
$output = $this->commandTester->getDisplay();
$this->assertEquals($expectedOutput, $output);
}

public function providesAppIds() {
return [
[
['--listNonCoreDirectories'],
'Non-App Directories:
- /apps-external -> /opt/core/apps-external
- /apps-external-2 -> /opt/core/apps-external-2
'
],
];
}
}

0 comments on commit 4fe6705

Please sign in to comment.