2013-08-17 05:16:48 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
2024-05-10 09:09:14 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2013-08-17 05:16:48 -04:00
|
|
|
*/
|
|
|
|
|
|
2016-05-18 12:40:34 -04:00
|
|
|
namespace Test\AppFramework;
|
2013-08-17 05:16:48 -04:00
|
|
|
|
2016-05-18 12:40:34 -04:00
|
|
|
use OC\AppFramework\App;
|
2025-06-12 12:31:58 -04:00
|
|
|
use OC\AppFramework\DependencyInjection\DIContainer;
|
2018-11-01 06:29:29 -04:00
|
|
|
use OC\AppFramework\Http\Dispatcher;
|
|
|
|
|
use OCP\AppFramework\Controller;
|
2025-06-12 12:31:58 -04:00
|
|
|
use OCP\AppFramework\Http\IOutput;
|
2015-01-22 14:26:46 -05:00
|
|
|
use OCP\AppFramework\Http\Response;
|
|
|
|
|
|
2014-12-13 13:28:20 -05:00
|
|
|
function rrmdir($directory) {
|
2020-03-26 04:30:18 -04:00
|
|
|
$files = array_diff(scandir($directory), ['.','..']);
|
2014-12-13 13:28:20 -05:00
|
|
|
foreach ($files as $file) {
|
|
|
|
|
if (is_dir($directory . '/' . $file)) {
|
|
|
|
|
rrmdir($directory . '/' . $file);
|
|
|
|
|
} else {
|
|
|
|
|
unlink($directory . '/' . $file);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return rmdir($directory);
|
|
|
|
|
}
|
2013-08-17 05:16:48 -04:00
|
|
|
|
2015-01-22 14:26:46 -05:00
|
|
|
|
2014-11-10 17:30:38 -05:00
|
|
|
class AppTest extends \Test\TestCase {
|
2025-06-17 09:00:01 -04:00
|
|
|
private DIContainer $container;
|
2015-01-22 14:26:46 -05:00
|
|
|
private $io;
|
2013-08-17 05:16:48 -04:00
|
|
|
private $api;
|
|
|
|
|
private $controller;
|
|
|
|
|
private $dispatcher;
|
|
|
|
|
private $params;
|
|
|
|
|
private $headers;
|
|
|
|
|
private $output;
|
|
|
|
|
private $controllerName;
|
|
|
|
|
private $controllerMethod;
|
2014-12-13 13:28:20 -05:00
|
|
|
private $appPath;
|
2013-08-17 05:16:48 -04:00
|
|
|
|
2019-11-21 10:40:38 -05:00
|
|
|
protected function setUp(): void {
|
2014-11-10 17:30:38 -05:00
|
|
|
parent::setUp();
|
|
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->container = new DIContainer('test', []);
|
2018-11-01 06:29:29 -04:00
|
|
|
$this->controller = $this->createMock(Controller::class);
|
|
|
|
|
$this->dispatcher = $this->createMock(Dispatcher::class);
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->io = $this->createMock(IOutput::class);
|
2013-08-17 05:16:48 -04:00
|
|
|
|
2020-03-26 04:30:18 -04:00
|
|
|
$this->headers = ['key' => 'value'];
|
2013-08-17 05:16:48 -04:00
|
|
|
$this->output = 'hi';
|
|
|
|
|
$this->controllerName = 'Controller';
|
|
|
|
|
$this->controllerMethod = 'method';
|
|
|
|
|
|
|
|
|
|
$this->container[$this->controllerName] = $this->controller;
|
2025-06-17 09:00:01 -04:00
|
|
|
$this->container[Dispatcher::class] = $this->dispatcher;
|
|
|
|
|
$this->container[IOutput::class] = $this->io;
|
2022-01-19 17:30:34 -05:00
|
|
|
$this->container['urlParams'] = ['_route' => 'not-profiler'];
|
2014-12-13 13:28:20 -05:00
|
|
|
|
2016-02-03 06:57:22 -05:00
|
|
|
$this->appPath = __DIR__ . '/../../../apps/namespacetestapp';
|
|
|
|
|
$infoXmlPath = $this->appPath . '/appinfo/info.xml';
|
|
|
|
|
mkdir($this->appPath . '/appinfo', 0777, true);
|
2014-12-13 13:28:20 -05:00
|
|
|
|
2025-06-30 09:04:05 -04:00
|
|
|
$xml = '<?xml version="1.0" encoding="UTF-8"?>'
|
|
|
|
|
. '<info>'
|
|
|
|
|
. '<id>namespacetestapp</id>'
|
|
|
|
|
. '<namespace>NameSpaceTestApp</namespace>'
|
|
|
|
|
. '</info>';
|
2014-12-13 13:28:20 -05:00
|
|
|
file_put_contents($infoXmlPath, $xml);
|
2013-08-17 05:16:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testControllerNameAndMethodAreBeingPassed(): void {
|
2019-05-24 07:00:17 -04:00
|
|
|
$return = ['HTTP/2.0 200 OK', [], [], null, new Response()];
|
2013-08-17 05:16:48 -04:00
|
|
|
$this->dispatcher->expects($this->once())
|
|
|
|
|
->method('dispatch')
|
|
|
|
|
->with($this->equalTo($this->controller),
|
|
|
|
|
$this->equalTo($this->controllerMethod))
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn($return);
|
2013-08-17 05:16:48 -04:00
|
|
|
|
2015-01-22 14:26:46 -05:00
|
|
|
$this->io->expects($this->never())
|
|
|
|
|
->method('setOutput');
|
2013-08-17 05:16:48 -04:00
|
|
|
|
2013-10-01 13:01:52 -04:00
|
|
|
App::main($this->controllerName, $this->controllerMethod,
|
2013-08-17 05:16:48 -04:00
|
|
|
$this->container);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testBuildAppNamespace(): void {
|
2014-12-13 13:28:20 -05:00
|
|
|
$ns = App::buildAppNamespace('someapp');
|
|
|
|
|
$this->assertEquals('OCA\Someapp', $ns);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testBuildAppNamespaceCore(): void {
|
2014-12-13 13:28:20 -05:00
|
|
|
$ns = App::buildAppNamespace('someapp', 'OC\\');
|
|
|
|
|
$this->assertEquals('OC\Someapp', $ns);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testBuildAppNamespaceInfoXml(): void {
|
2014-12-13 13:28:20 -05:00
|
|
|
$ns = App::buildAppNamespace('namespacetestapp', 'OCA\\');
|
|
|
|
|
$this->assertEquals('OCA\NameSpaceTestApp', $ns);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-11-21 10:40:38 -05:00
|
|
|
protected function tearDown(): void {
|
2014-12-13 13:28:20 -05:00
|
|
|
rrmdir($this->appPath);
|
2015-11-02 19:52:41 -05:00
|
|
|
parent::tearDown();
|
2014-12-13 13:28:20 -05:00
|
|
|
}
|
|
|
|
|
|
2013-08-17 05:16:48 -04:00
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testOutputIsPrinted(): void {
|
2019-05-24 07:00:17 -04:00
|
|
|
$return = ['HTTP/2.0 200 OK', [], [], $this->output, new Response()];
|
2013-08-17 05:16:48 -04:00
|
|
|
$this->dispatcher->expects($this->once())
|
|
|
|
|
->method('dispatch')
|
|
|
|
|
->with($this->equalTo($this->controller),
|
|
|
|
|
$this->equalTo($this->controllerMethod))
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn($return);
|
2015-01-22 14:26:46 -05:00
|
|
|
$this->io->expects($this->once())
|
|
|
|
|
->method('setOutput')
|
|
|
|
|
->with($this->equalTo($this->output));
|
|
|
|
|
App::main($this->controllerName, $this->controllerMethod, $this->container, []);
|
2013-08-17 05:16:48 -04:00
|
|
|
}
|
|
|
|
|
|
2025-05-12 11:18:04 -04:00
|
|
|
public static function dataNoOutput(): array {
|
2016-08-31 17:07:48 -04:00
|
|
|
return [
|
2019-05-24 07:00:17 -04:00
|
|
|
['HTTP/2.0 204 No content'],
|
|
|
|
|
['HTTP/2.0 304 Not modified'],
|
2016-08-31 17:07:48 -04:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 10:56:59 -04:00
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('dataNoOutput')]
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testNoOutput(string $statusCode): void {
|
2016-08-31 17:07:48 -04:00
|
|
|
$return = [$statusCode, [], [], $this->output, new Response()];
|
|
|
|
|
$this->dispatcher->expects($this->once())
|
|
|
|
|
->method('dispatch')
|
|
|
|
|
->with($this->equalTo($this->controller),
|
|
|
|
|
$this->equalTo($this->controllerMethod))
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn($return);
|
2016-08-31 17:07:48 -04:00
|
|
|
$this->io->expects($this->once())
|
|
|
|
|
->method('setHeader')
|
|
|
|
|
->with($this->equalTo($statusCode));
|
|
|
|
|
$this->io->expects($this->never())
|
|
|
|
|
->method('setOutput');
|
|
|
|
|
App::main($this->controllerName, $this->controllerMethod, $this->container, []);
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-17 05:16:48 -04:00
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testCallbackIsCalled(): void {
|
2015-01-22 14:26:46 -05:00
|
|
|
$mock = $this->getMockBuilder('OCP\AppFramework\Http\ICallbackResponse')
|
|
|
|
|
->getMock();
|
|
|
|
|
|
2019-05-24 07:00:17 -04:00
|
|
|
$return = ['HTTP/2.0 200 OK', [], [], $this->output, $mock];
|
2015-01-22 14:26:46 -05:00
|
|
|
$this->dispatcher->expects($this->once())
|
|
|
|
|
->method('dispatch')
|
|
|
|
|
->with($this->equalTo($this->controller),
|
|
|
|
|
$this->equalTo($this->controllerMethod))
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn($return);
|
2015-01-22 14:26:46 -05:00
|
|
|
$mock->expects($this->once())
|
|
|
|
|
->method('callback');
|
|
|
|
|
App::main($this->controllerName, $this->controllerMethod, $this->container, []);
|
|
|
|
|
}
|
2013-08-17 05:16:48 -04:00
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testCoreApp(): void {
|
2025-06-17 09:00:01 -04:00
|
|
|
$this->container['appName'] = 'core';
|
2016-08-08 14:38:10 -04:00
|
|
|
$this->container['OC\Core\Controller\Foo'] = $this->controller;
|
2022-01-19 17:30:34 -05:00
|
|
|
$this->container['urlParams'] = ['_route' => 'not-profiler'];
|
2016-08-08 14:38:10 -04:00
|
|
|
|
2019-05-24 07:00:17 -04:00
|
|
|
$return = ['HTTP/2.0 200 OK', [], [], null, new Response()];
|
2016-08-08 14:38:10 -04:00
|
|
|
$this->dispatcher->expects($this->once())
|
|
|
|
|
->method('dispatch')
|
|
|
|
|
->with($this->equalTo($this->controller),
|
|
|
|
|
$this->equalTo($this->controllerMethod))
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn($return);
|
2016-08-08 14:38:10 -04:00
|
|
|
|
|
|
|
|
$this->io->expects($this->never())
|
|
|
|
|
->method('setOutput');
|
|
|
|
|
|
|
|
|
|
App::main('Foo', $this->controllerMethod, $this->container);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testSettingsApp(): void {
|
2025-06-17 09:00:01 -04:00
|
|
|
$this->container['appName'] = 'settings';
|
2019-09-17 10:33:27 -04:00
|
|
|
$this->container['OCA\Settings\Controller\Foo'] = $this->controller;
|
2022-01-19 17:30:34 -05:00
|
|
|
$this->container['urlParams'] = ['_route' => 'not-profiler'];
|
2016-08-08 14:38:10 -04:00
|
|
|
|
2019-05-24 07:00:17 -04:00
|
|
|
$return = ['HTTP/2.0 200 OK', [], [], null, new Response()];
|
2016-08-08 14:38:10 -04:00
|
|
|
$this->dispatcher->expects($this->once())
|
|
|
|
|
->method('dispatch')
|
|
|
|
|
->with($this->equalTo($this->controller),
|
|
|
|
|
$this->equalTo($this->controllerMethod))
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn($return);
|
2016-08-08 14:38:10 -04:00
|
|
|
|
|
|
|
|
$this->io->expects($this->never())
|
|
|
|
|
->method('setOutput');
|
|
|
|
|
|
|
|
|
|
App::main('Foo', $this->controllerMethod, $this->container);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testApp(): void {
|
2025-06-17 09:00:01 -04:00
|
|
|
$this->container['appName'] = 'bar';
|
2016-08-08 14:38:10 -04:00
|
|
|
$this->container['OCA\Bar\Controller\Foo'] = $this->controller;
|
2022-01-19 17:30:34 -05:00
|
|
|
$this->container['urlParams'] = ['_route' => 'not-profiler'];
|
2016-08-08 14:38:10 -04:00
|
|
|
|
2019-05-24 07:00:17 -04:00
|
|
|
$return = ['HTTP/2.0 200 OK', [], [], null, new Response()];
|
2016-08-08 14:38:10 -04:00
|
|
|
$this->dispatcher->expects($this->once())
|
|
|
|
|
->method('dispatch')
|
|
|
|
|
->with($this->equalTo($this->controller),
|
|
|
|
|
$this->equalTo($this->controllerMethod))
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn($return);
|
2016-08-08 14:38:10 -04:00
|
|
|
|
|
|
|
|
$this->io->expects($this->never())
|
|
|
|
|
->method('setOutput');
|
|
|
|
|
|
|
|
|
|
App::main('Foo', $this->controllerMethod, $this->container);
|
|
|
|
|
}
|
2013-08-17 05:16:48 -04:00
|
|
|
}
|