Skip to content

Commit

Permalink
Add unittests for webdav application
Browse files Browse the repository at this point in the history
  • Loading branch information
usox committed Aug 29, 2020
1 parent a41ccfd commit bf11415
Show file tree
Hide file tree
Showing 24 changed files with 544 additions and 59 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ cs_fixer_tmp_*
.pc
.php_cs.cache
.project
build
php-cs-fixer.phar
composer.phar
robots.txt
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ before_script:
script:
- resources/scripts/tests/syntax.sh
- resources/scripts/tests/codestyle.sh
- composer test
- composer tests
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@
}
},
"scripts": {
"test": "./vendor/bin/phpunit tests",
"coverage": "./vendor/bin/phpunit --warm-coverage-cache && ./vendor/bin/phpunit --coverage-html build/coverage tests",
"tests": "./vendor/bin/phpunit tests",
"codestyle": "resources/scripts/tests/codestyle.sh",
"syntax": "resources/scripts/tests/syntax.sh",
"fix-cs": "vendor/bin/php-cs-fixer fix -v ."
Expand Down
25 changes: 25 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0"?>
<phpunit
xmlns:xsi="http:https://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
backupGlobals="true"
backupStaticAttributes="false"
bootstrap="tests/bootstrap.php"
cacheResult="true"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
forceCoversAnnotation="false"
processIsolation="false"
stopOnError="true"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
stopOnRisky="false">
<coverage cacheDirectory="build/coverageCache">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit>
50 changes: 34 additions & 16 deletions src/Application/Api/WebDavApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,50 @@
namespace Ampache\Application\Api;

use Ampache\Application\ApplicationInterface;
use AmpConfig;
use Sabre\DAV\Auth\Plugin;
use Sabre\DAV\Server;
use Ampache\Module\WebDav\WebDav_Auth;
use Ampache\Module\WebDav\WebDav_Catalog;
use Ampache\Config\ConfigContainerInterface;
use Ampache\Module\WebDav\WebDavFactoryInterface;

final class WebDavApplication implements ApplicationInterface
{
private ConfigContainerInterface $configContainer;

private WebDavFactoryInterface $webDavFactory;

public function __construct(
ConfigContainerInterface $configContainer,
WebDavFactoryInterface $webDavFactory
) {
$this->configContainer = $configContainer;
$this->webDavFactory = $webDavFactory;
}

public function run(): void
{
if (!AmpConfig::get('webdav_backend')) {
echo T_("Disabled");
if ($this->configContainer->isWebDavBackendEnabled() === false) {
echo T_('Disabled');

return;
}

$rootDir = new WebDav_Catalog();
$server = new Server($rootDir);
$server = $this->webDavFactory->createServer(
$this->webDavFactory->createWebDavCatalog()
);

$raw_web_path = $this->configContainer->getRawWebPath();
if ($raw_web_path === '/') {
$raw_web_path = '';
}

$server->setBaseUri(
sprintf('%s/webdav/index.php', $raw_web_path)
);

$baseUri = ((AmpConfig::get('raw_web_path') !== "/") ? AmpConfig::get('raw_web_path') : "") . '/webdav/index.php';
$server->setBaseUri($baseUri);
if (AmpConfig::get('use_auth')) {
$authBackend = new WebDav_Auth();
$authBackend->setRealm('Ampache');
$authPlugin = new Plugin($authBackend);
$server->addPlugin($authPlugin);
if ($this->configContainer->isAuthenticationEnabled()) {
$server->addPlugin(
$this->webDavFactory->createPlugin(
$this->webDavFactory->createWebDavAuth()
)
);
}

$server->exec();
Expand Down
File renamed without changes.
5 changes: 3 additions & 2 deletions src/Config/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@
}),
]);
$builder->addDefinitions(
require_once __DIR__ . '/../Application/ServiceDefinition.php',
require_once __DIR__ . '/../Module/Util/ServiceDefinition.php',
require_once __DIR__ . '/../Application/service_definition.php',
require_once __DIR__ . '/../Module/Util/service_definition.php',
require_once __DIR__ . '/../Module/WebDav/service_definition.php',
);

return $builder->build();
15 changes: 15 additions & 0 deletions src/Config/ConfigContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,19 @@ public function getSessionName(): string
{
return $this->configuration[ConfigurationKeyEnum::SESSION_NAME] ?? '';
}

public function isWebDavBackendEnabled(): bool
{
return (bool) ($this->configuration[ConfigurationKeyEnum::BACKEND_WEBDAV] ?? false);
}

public function isAuthenticationEnabled(): bool
{
return (bool) ($this->configuration[ConfigurationKeyEnum::USE_AUTH] ?? true);
}

public function getRawWebPath(): string
{
return $this->configuration[ConfigurationKeyEnum::RAW_WEB_PATH] ?? '';
}
}
15 changes: 15 additions & 0 deletions src/Config/ConfigContainerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,19 @@ public function get(string $configKey);
* Returns the name of the PHP session
*/
public function getSessionName(): string;

/**
* Returns the webdav config state
*/
public function isWebDavBackendEnabled(): bool;

/**
* Returns the authentication config state
*/
public function isAuthenticationEnabled(): bool;

/**
* Returns the raw web path
*/
public function getRawWebPath(): string;
}
5 changes: 4 additions & 1 deletion src/Config/ConfigurationKeyEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@
*/
final class ConfigurationKeyEnum
{
public const SESSION_NAME = 'session_name';
public const SESSION_NAME = 'session_name';
public const BACKEND_WEBDAV = 'webdav_backend';
public const RAW_WEB_PATH = 'raw_web_path';
public const USE_AUTH = 'use_auth';
}
3 changes: 2 additions & 1 deletion src/Config/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,8 @@ function apache_request_headers()

return $headers;
}

}
if (!function_exists('getallheaders')) {
/**
* @return array
*/
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
use Sabre\DAV;

/**
* WebDAV Directory Class
*
* This class wrap Ampache albums and artist to WebDAV directories.
*/
class WebDav_Auth extends DAV\Auth\Backend\AbstractBasic
final class WebDavAuth extends DAV\Auth\Backend\AbstractBasic
{
protected $realm = 'Ampache';

/**
* validateUserPass
* @param $username
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,11 @@
*
* This class wrap Ampache catalogs to WebDAV directories.
*/
class WebDav_Catalog extends DAV\Collection
class WebDavCatalog extends DAV\Collection
{
private $catalog_id;
private int $catalog_id;

/**
* @param integer $catalog_id
*/
public function __construct($catalog_id = 0)
public function __construct(int $catalog_id)
{
$this->catalog_id = $catalog_id;
}
Expand All @@ -59,7 +56,7 @@ public function getChildren()
}
$artists = Catalog::get_artists($catalogs);
foreach ($artists as $artist) {
$children[] = new WebDav_Directory($artist);
$children[] = new WebDavDirectory($artist);
}

return $children;
Expand All @@ -68,7 +65,7 @@ public function getChildren()
/**
* getChild
* @param string $name
* @return WebDav_File|WebDav_Directory
* @return WebDavFile|WebDavDirectory
*/
public function getChild($name)
{
Expand All @@ -78,7 +75,7 @@ public function getChild($name)
// Always return first match
// Warning: this means that two items with the same name will not be supported for now
if (count($matches) > 0) {
return WebDav_Directory::getChildFromArray($matches[0]);
return WebDavDirectory::getChildFromArray($matches[0]);
}

throw new DAV\Exception\NotFound('The artist with name: ' . $name . ' could not be found');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
/**
* This class wrap Ampache albums and artist to WebDAV directories.
*/
class WebDav_Directory extends DAV\Collection
class WebDavDirectory extends DAV\Collection
{
private $libitem;

Expand All @@ -56,10 +56,10 @@ public function getChildren()
foreach ($childs as $key => $child) {
if (is_string($key)) {
foreach ($child as $schild) {
$children[] = WebDav_Directory::getChildFromArray($schild);
$children[] = WebDavDirectory::getChildFromArray($schild);
}
} else {
$children[] = WebDav_Directory::getChildFromArray($child);
$children[] = WebDavDirectory::getChildFromArray($child);
}
}

Expand All @@ -69,7 +69,7 @@ public function getChildren()
/**
* getChild
* @param string $name
* @return WebDav_File|WebDav_Directory
* @return WebDavFile|WebDavDirectory
*/
public function getChild($name)
{
Expand All @@ -85,7 +85,7 @@ public function getChild($name)
// Always return first match
// Warning: this means that two items with the same name will not be supported for now
if (count($matches) > 0) {
return WebDav_Directory::getChildFromArray($matches[0]);
return WebDavDirectory::getChildFromArray($matches[0]);
}

throw new DAV\Exception\NotFound('The child with name: ' . $name . ' could not be found');
Expand All @@ -94,7 +94,7 @@ public function getChild($name)
/**
* getChildFromArray
* @param $array
* @return WebDav_File|WebDav_Directory
* @return WebDavFile|WebDavDirectory
*/
public static function getChildFromArray($array)
{
Expand All @@ -104,9 +104,9 @@ public static function getChildFromArray($array)
}

if ($libitem instanceof Media) {
return new WebDav_File($libitem);
return new WebDavFile($libitem);
} else {
return new WebDav_Directory($libitem);
return new WebDavDirectory($libitem);
}
}

Expand Down
60 changes: 60 additions & 0 deletions src/Module/WebDav/WebDavFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/*
* vim:set softtabstop=4 shiftwidth=4 expandtab:
*
* LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later)
* Copyright 2001 - 2020 Ampache.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/

declare(strict_types=1);

namespace Ampache\Module\WebDav;

use Sabre\DAV\Auth\Backend\BackendInterface;
use Sabre\DAV\Auth\Plugin;
use Sabre\DAV\Exception;
use Sabre\DAV\INode;
use Sabre\DAV\Server;
use Sabre\DAV\Tree;

final class WebDavFactory implements WebDavFactoryInterface
{
public function createWebDavAuth(): WebDavAuth
{
return new WebDavAuth();
}

public function createWebDavCatalog(int $catalog_id = 0): WebDavCatalog
{
return new WebDavCatalog($catalog_id);
}

/**
* @param Tree|INode|array|null $node The tree object
*
* @throws Exception
*/
public function createServer($node): Server
{
return new Server($node);
}

public function createPlugin(?BackendInterface $backend): Plugin
{
return new Plugin($backend);
}
}
Loading

0 comments on commit bf11415

Please sign in to comment.