2017-01-16 16:19:19 +00:00
|
|
|
<?php
|
2019-10-03 19:24:12 +00:00
|
|
|
|
2017-01-16 16:19:19 +00:00
|
|
|
/**
|
|
|
|
* FrontControllerTest class.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Alltube\Test;
|
|
|
|
|
2017-01-16 16:31:20 +00:00
|
|
|
use Alltube\Controller\FrontController;
|
2020-06-20 23:44:20 +00:00
|
|
|
use Alltube\Exception\ConfigException;
|
2020-10-21 21:07:12 +00:00
|
|
|
use Alltube\Exception\DependencyException;
|
2020-06-20 23:44:20 +00:00
|
|
|
use Alltube\Library\Exception\AlltubeLibraryException;
|
2018-02-05 15:48:58 +00:00
|
|
|
use Exception;
|
2022-02-27 09:54:56 +00:00
|
|
|
use Graby\HttpClient\Plugin\ServerSideRequestForgeryProtection\Exception\InvalidURLException;
|
2017-01-16 16:19:19 +00:00
|
|
|
use Slim\Http\Environment;
|
|
|
|
use Slim\Http\Request;
|
2020-06-20 23:44:20 +00:00
|
|
|
use SmartyException;
|
2017-01-16 16:19:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Unit tests for the FrontController class.
|
|
|
|
*/
|
2019-04-22 14:05:58 +00:00
|
|
|
class FrontControllerTest extends ControllerTest
|
2017-01-16 16:19:19 +00:00
|
|
|
{
|
2020-05-13 20:28:05 +00:00
|
|
|
/**
|
|
|
|
* Controller instance used in tests.
|
|
|
|
* @var FrontController
|
|
|
|
*/
|
|
|
|
protected $controller;
|
|
|
|
|
2017-01-16 16:19:19 +00:00
|
|
|
/**
|
|
|
|
* Prepare tests.
|
2020-10-20 21:29:50 +00:00
|
|
|
* @throws ConfigException|SmartyException
|
2020-10-21 21:07:12 +00:00
|
|
|
* @throws DependencyException
|
2017-01-16 16:19:19 +00:00
|
|
|
*/
|
2019-11-30 13:08:18 +00:00
|
|
|
protected function setUp(): void
|
2017-01-16 16:19:19 +00:00
|
|
|
{
|
2019-04-21 16:30:02 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->controller = new FrontController($this->container);
|
2018-03-20 11:02:21 +00:00
|
|
|
}
|
|
|
|
|
2017-04-24 16:07:59 +00:00
|
|
|
/**
|
|
|
|
* Test the constructor.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testConstructor()
|
2017-11-11 12:58:55 +00:00
|
|
|
{
|
2019-04-21 16:30:02 +00:00
|
|
|
$this->assertInstanceOf(FrontController::class, new FrontController($this->container));
|
2017-04-24 16:07:59 +00:00
|
|
|
}
|
|
|
|
|
2017-01-16 16:19:19 +00:00
|
|
|
/**
|
2017-01-16 16:31:20 +00:00
|
|
|
* Test the constructor with streams enabled.
|
|
|
|
*
|
2017-01-16 16:19:19 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testConstructorWithStream()
|
|
|
|
{
|
2020-10-17 20:07:07 +00:00
|
|
|
$config = $this->container->get('config');
|
|
|
|
$config->setOptions(['stream' => true]);
|
2019-04-21 16:30:02 +00:00
|
|
|
$this->assertInstanceOf(FrontController::class, new FrontController($this->container));
|
2017-01-16 16:19:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-16 16:31:20 +00:00
|
|
|
* Test the index() function.
|
|
|
|
*
|
2017-01-16 16:19:19 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testIndex()
|
|
|
|
{
|
2017-04-25 21:49:13 +00:00
|
|
|
$this->assertRequestIsOk('index');
|
2017-01-16 16:19:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-16 16:31:20 +00:00
|
|
|
* Test the index() function with a custom URI.
|
|
|
|
*
|
2017-01-16 16:19:19 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testIndexWithCustomUri()
|
|
|
|
{
|
|
|
|
$result = $this->controller->index(
|
|
|
|
Request::createFromEnvironment(
|
2017-10-29 22:21:13 +00:00
|
|
|
Environment::mock(['REQUEST_URI' => '/foo', 'QUERY_STRING' => 'foo=bar'])
|
2017-01-16 16:19:19 +00:00
|
|
|
),
|
2020-10-22 20:39:09 +00:00
|
|
|
$this->container->get('response')
|
2017-01-16 16:19:19 +00:00
|
|
|
);
|
|
|
|
$this->assertTrue($result->isOk());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-16 16:31:20 +00:00
|
|
|
* Test the extractors() function.
|
|
|
|
*
|
2017-01-16 16:19:19 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testExtractors()
|
|
|
|
{
|
2017-04-25 21:49:13 +00:00
|
|
|
$this->assertRequestIsOk('extractors');
|
2017-01-16 16:19:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-16 16:31:20 +00:00
|
|
|
* Test the password() function.
|
|
|
|
*
|
2017-01-16 16:19:19 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testPassword()
|
|
|
|
{
|
2020-06-20 23:44:20 +00:00
|
|
|
$this->assertRequestIsClientError('password');
|
2017-01-16 16:19:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-22 13:20:05 +00:00
|
|
|
* Test the info() function without the url parameter.
|
2017-01-16 16:31:20 +00:00
|
|
|
*
|
2017-01-16 16:19:19 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2019-04-22 13:20:05 +00:00
|
|
|
public function testInfoWithoutUrl()
|
2017-01-16 16:19:19 +00:00
|
|
|
{
|
2022-02-27 09:54:56 +00:00
|
|
|
$this->expectException(InvalidURLException::class);
|
|
|
|
$this->getRequestResult('info', []);
|
2017-01-16 16:19:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-22 13:20:05 +00:00
|
|
|
* Test the info() function.
|
2017-01-16 16:31:20 +00:00
|
|
|
*
|
2017-01-16 16:19:19 +00:00
|
|
|
* @return void
|
2019-10-26 14:13:08 +00:00
|
|
|
* @requires download
|
2017-01-16 16:19:19 +00:00
|
|
|
*/
|
2019-04-22 13:20:05 +00:00
|
|
|
public function testInfo()
|
2017-01-16 16:19:19 +00:00
|
|
|
{
|
2019-04-22 13:20:05 +00:00
|
|
|
$this->assertRequestIsOk('info', ['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU']);
|
2017-01-16 16:19:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-22 13:20:05 +00:00
|
|
|
* Test the info() function with audio conversion.
|
2017-01-16 16:31:20 +00:00
|
|
|
*
|
2017-01-16 16:19:19 +00:00
|
|
|
* @return void
|
2019-10-26 14:13:08 +00:00
|
|
|
* @requires download
|
2017-01-16 16:19:19 +00:00
|
|
|
*/
|
2019-04-22 13:20:05 +00:00
|
|
|
public function testInfoWithAudio()
|
2017-01-16 16:19:19 +00:00
|
|
|
{
|
2020-10-17 20:07:07 +00:00
|
|
|
$config = $this->container->get('config');
|
|
|
|
$config->setOptions(['convert' => true]);
|
2019-04-21 21:26:11 +00:00
|
|
|
|
2019-04-22 14:05:58 +00:00
|
|
|
$this->assertRequestIsRedirect(
|
|
|
|
'info',
|
|
|
|
['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'audio' => true]
|
|
|
|
);
|
2017-01-16 16:19:19 +00:00
|
|
|
}
|
|
|
|
|
2018-05-01 14:43:22 +00:00
|
|
|
/**
|
2019-04-22 13:20:05 +00:00
|
|
|
* Test the info() function with audio conversion from a Vimeo video.
|
2018-05-01 14:43:22 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2019-10-26 14:13:08 +00:00
|
|
|
* @requires download
|
2018-05-01 14:43:22 +00:00
|
|
|
*/
|
2019-04-22 13:20:05 +00:00
|
|
|
public function testInfoWithVimeoAudio()
|
2018-05-01 14:43:22 +00:00
|
|
|
{
|
2020-10-17 20:07:07 +00:00
|
|
|
$config = $this->container->get('config');
|
|
|
|
$config->setOptions(['convert' => true]);
|
2019-04-21 21:26:11 +00:00
|
|
|
|
2018-05-01 14:43:22 +00:00
|
|
|
// So we can test the fallback to default format
|
2019-04-22 13:20:05 +00:00
|
|
|
$this->assertRequestIsRedirect('info', ['url' => 'https://vimeo.com/251997032', 'audio' => true]);
|
2018-05-01 14:43:22 +00:00
|
|
|
}
|
|
|
|
|
2017-01-16 16:19:19 +00:00
|
|
|
/**
|
2019-04-22 13:20:05 +00:00
|
|
|
* Test the info() function with audio enabled and an URL that doesn't need to be converted.
|
2017-01-16 16:31:20 +00:00
|
|
|
*
|
2017-01-16 16:19:19 +00:00
|
|
|
* @return void
|
2019-10-26 14:13:08 +00:00
|
|
|
* @requires download
|
2017-01-16 16:19:19 +00:00
|
|
|
*/
|
2019-04-22 13:20:05 +00:00
|
|
|
public function testInfoWithUnconvertedAudio()
|
2017-01-16 16:19:19 +00:00
|
|
|
{
|
2020-10-17 20:07:07 +00:00
|
|
|
$config = $this->container->get('config');
|
|
|
|
$config->setOptions(['convert' => true]);
|
2019-04-21 21:26:11 +00:00
|
|
|
|
2017-04-25 21:49:13 +00:00
|
|
|
$this->assertRequestIsRedirect(
|
2019-04-22 13:20:05 +00:00
|
|
|
'info',
|
2017-11-10 11:19:04 +00:00
|
|
|
[
|
|
|
|
'url' => 'https://2080.bandcamp.com/track/cygnus-x-the-orange-theme-2080-faulty-chip-cover',
|
|
|
|
'audio' => true,
|
|
|
|
]
|
2017-01-16 16:19:19 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-22 13:20:05 +00:00
|
|
|
* Test the info() function with a password.
|
2017-01-16 16:31:20 +00:00
|
|
|
*
|
2017-01-16 16:19:19 +00:00
|
|
|
* @return void
|
2019-10-26 14:13:08 +00:00
|
|
|
* @requires download
|
2022-02-27 22:45:59 +00:00
|
|
|
* @throws AlltubeLibraryException|InvalidURLException
|
2017-01-16 16:19:19 +00:00
|
|
|
*/
|
2019-04-22 13:20:05 +00:00
|
|
|
public function testInfoWithPassword()
|
2017-01-16 16:19:19 +00:00
|
|
|
{
|
2019-04-22 13:20:05 +00:00
|
|
|
$result = $this->controller->info(
|
2022-02-27 22:44:36 +00:00
|
|
|
$this->container->get('request')->withQueryParams(['url' => 'https://vimeo.com/68375962'])
|
2017-10-29 22:21:13 +00:00
|
|
|
->withParsedBody(['password' => 'youtube-dl']),
|
2020-10-22 20:39:09 +00:00
|
|
|
$this->container->get('response')
|
2017-01-16 16:19:19 +00:00
|
|
|
);
|
|
|
|
$this->assertTrue($result->isOk());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-22 13:20:05 +00:00
|
|
|
* Test the info() function with a missing password.
|
2017-01-16 16:31:20 +00:00
|
|
|
*
|
2017-01-16 16:19:19 +00:00
|
|
|
* @return void
|
2019-10-26 14:13:08 +00:00
|
|
|
* @requires download
|
2017-01-16 16:19:19 +00:00
|
|
|
*/
|
2019-04-22 13:20:05 +00:00
|
|
|
public function testInfoWithMissingPassword()
|
2017-01-16 16:19:19 +00:00
|
|
|
{
|
2022-02-27 22:44:36 +00:00
|
|
|
$this->assertRequestIsClientError('info', ['url' => 'https://vimeo.com/68375962']);
|
|
|
|
$this->assertRequestIsClientError('info', ['url' => 'https://vimeo.com/68375962', 'audio' => true]);
|
2017-01-16 16:19:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-22 13:20:05 +00:00
|
|
|
* Test the info() function with streams enabled.
|
2017-01-16 16:31:20 +00:00
|
|
|
*
|
2017-01-16 16:19:19 +00:00
|
|
|
* @return void
|
2019-10-26 14:13:08 +00:00
|
|
|
* @requires download
|
2017-01-16 16:19:19 +00:00
|
|
|
*/
|
2019-04-22 13:20:05 +00:00
|
|
|
public function testInfoWithStream()
|
2017-01-16 16:19:19 +00:00
|
|
|
{
|
2020-10-17 20:07:07 +00:00
|
|
|
$config = $this->container->get('config');
|
|
|
|
$config->setOptions(['stream' => true]);
|
2019-04-21 16:30:02 +00:00
|
|
|
|
2019-04-22 13:20:05 +00:00
|
|
|
$this->assertRequestIsOk('info', ['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU']);
|
2017-04-25 21:49:13 +00:00
|
|
|
$this->assertRequestIsOk(
|
2019-04-22 13:20:05 +00:00
|
|
|
'info',
|
2019-04-21 16:30:02 +00:00
|
|
|
['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'audio' => true]
|
2017-01-16 16:19:19 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-04-24 23:53:38 +00:00
|
|
|
/**
|
2019-04-22 13:20:05 +00:00
|
|
|
* Test the info() function with a playlist.
|
2017-04-24 23:53:38 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2019-10-26 14:13:08 +00:00
|
|
|
* @requires download
|
2017-04-24 23:53:38 +00:00
|
|
|
*/
|
2019-04-22 13:20:05 +00:00
|
|
|
public function testInfoWithPlaylist()
|
2017-04-24 23:53:38 +00:00
|
|
|
{
|
2017-04-25 21:49:13 +00:00
|
|
|
$this->assertRequestIsOk(
|
2019-04-22 13:20:05 +00:00
|
|
|
'info',
|
2017-10-29 22:21:13 +00:00
|
|
|
['url' => 'https://www.youtube.com/playlist?list=PLgdySZU6KUXL_8Jq5aUkyNV7wCa-4wZsC']
|
2017-04-24 23:53:38 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-16 16:19:19 +00:00
|
|
|
/**
|
2017-01-16 16:31:20 +00:00
|
|
|
* Test the error() function.
|
|
|
|
*
|
2017-01-16 16:19:19 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testError()
|
|
|
|
{
|
2020-10-22 20:39:09 +00:00
|
|
|
$result = $this->controller->error(
|
|
|
|
$this->container->get('request'),
|
|
|
|
$this->container->get('response'),
|
|
|
|
new Exception('foo')
|
|
|
|
);
|
2017-01-16 16:19:19 +00:00
|
|
|
$this->assertTrue($result->isServerError());
|
|
|
|
}
|
|
|
|
|
2017-05-30 22:48:50 +00:00
|
|
|
/**
|
|
|
|
* Test the locale() function.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testLocale()
|
|
|
|
{
|
|
|
|
$this->assertTrue(
|
|
|
|
$this->controller->locale(
|
2020-10-22 20:39:09 +00:00
|
|
|
$this->container->get('request'),
|
|
|
|
$this->container->get('response'),
|
2017-10-29 22:21:13 +00:00
|
|
|
['locale' => 'fr_FR']
|
2017-05-30 22:48:50 +00:00
|
|
|
)->isRedirect()
|
|
|
|
);
|
|
|
|
}
|
2017-01-16 16:19:19 +00:00
|
|
|
}
|