View source
<?php
namespace Drupal\Tests\media_library\Kernel;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\KernelTests\KernelTestBase;
use Drupal\media_library\MediaLibraryState;
use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class MediaLibraryStateTest extends KernelTestBase {
use MediaTypeCreationTrait;
protected static $modules = [
'media',
'media_library',
'file',
'field',
'image',
'system',
'views',
'user',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('user');
$this
->installEntitySchema('file');
$this
->installSchema('file', 'file_usage');
$this
->installSchema('system', 'sequences');
$this
->installEntitySchema('media');
$this
->installConfig([
'field',
'system',
'file',
'image',
'media',
'media_library',
]);
$this
->createMediaType('file', [
'id' => 'document',
]);
$this
->createMediaType('image', [
'id' => 'image',
]);
$this
->createMediaType('video_file', [
'id' => 'video',
]);
}
public function testMethods() {
$opener_id = 'test';
$allowed_media_type_ids = [
'document',
'image',
];
$selected_media_type_id = 'image';
$remaining_slots = 2;
$state = MediaLibraryState::create($opener_id, $allowed_media_type_ids, $selected_media_type_id, $remaining_slots);
$this
->assertSame($opener_id, $state
->getOpenerId());
$this
->assertSame($allowed_media_type_ids, $state
->getAllowedTypeIds());
$this
->assertSame($selected_media_type_id, $state
->getSelectedTypeId());
$this
->assertSame($remaining_slots, $state
->getAvailableSlots());
$this
->assertTrue($state
->hasSlotsAvailable());
$state = MediaLibraryState::create($opener_id, $allowed_media_type_ids, $selected_media_type_id, 0);
$this
->assertFalse($state
->hasSlotsAvailable());
}
public function testCreate($opener_id, array $allowed_media_type_ids, $selected_type_id, $remaining_slots, $exception_message = '') {
if ($exception_message) {
$this
->expectException(\InvalidArgumentException::class);
$this
->expectExceptionMessage($exception_message);
}
$state = MediaLibraryState::create($opener_id, $allowed_media_type_ids, $selected_type_id, $remaining_slots);
$this
->assertInstanceOf(MediaLibraryState::class, $state);
$this
->assertInstanceOf(CacheableDependencyInterface::class, $state);
$this
->assertSame([
'url.query_args',
], $state
->getCacheContexts());
$this
->assertSame(Cache::PERMANENT, $state
->getCacheMaxAge());
}
public function providerCreate() {
$test_data = [];
$test_data['valid parameters'] = [
'test',
[
'document',
'image',
],
'image',
2,
];
$test_data['empty opener ID'] = [
'',
[
'document',
'image',
],
'image',
2,
'The opener ID parameter is required and must be a string.',
];
$test_data['integer opener ID'] = [
1,
[
'document',
'image',
],
'image',
2,
'The opener ID parameter is required and must be a string.',
];
$test_data['boolean opener ID'] = [
TRUE,
[
'document',
'image',
],
'image',
2,
'The opener ID parameter is required and must be a string.',
];
$test_data['spaces opener ID'] = [
' ',
[
'document',
'image',
],
'image',
2,
'The opener ID parameter is required and must be a string.',
];
$test_data['empty allowed types'] = [
'test',
[],
'image',
2,
'The allowed types parameter is required and must be an array of strings.',
];
$test_data['integer in allowed types'] = [
'test',
[
1,
'image',
],
'image',
2,
'The allowed types parameter is required and must be an array of strings.',
];
$test_data['boolean in allowed types'] = [
'test',
[
TRUE,
'image',
],
'image',
2,
'The allowed types parameter is required and must be an array of strings.',
];
$test_data['spaces in allowed types'] = [
'test',
[
' ',
'image',
],
'image',
2,
'The allowed types parameter is required and must be an array of strings.',
];
$test_data['empty selected type'] = [
'test',
[
'document',
'image',
],
'',
2,
'The selected type parameter is required and must be a string.',
];
$test_data['numeric selected type'] = [
'test',
[
'document',
'image',
],
1,
2,
'The selected type parameter is required and must be a string.',
];
$test_data['boolean selected type'] = [
'test',
[
'document',
'image',
],
TRUE,
2,
'The selected type parameter is required and must be a string.',
];
$test_data['spaces selected type'] = [
'test',
[
'document',
'image',
],
' ',
2,
'The selected type parameter is required and must be a string.',
];
$test_data['non-present selected type'] = [
'test',
[
'document',
'image',
],
'video',
2,
'The selected type parameter must be present in the list of allowed types.',
];
$test_data['empty remaining slots'] = [
'test',
[
'document',
'image',
],
'image',
'',
'The remaining slots parameter is required and must be numeric.',
];
$test_data['string remaining slots'] = [
'test',
[
'document',
'image',
],
'image',
'fail',
'The remaining slots parameter is required and must be numeric.',
];
$test_data['boolean remaining slots'] = [
'test',
[
'document',
'image',
],
'image',
TRUE,
'The remaining slots parameter is required and must be numeric.',
];
return $test_data;
}
public function testFromRequest(array $query_overrides, $exception_expected) {
$query = MediaLibraryState::create('test', [
'file',
'image',
], 'image', 2)
->all();
$query = array_merge($query, $query_overrides);
if ($exception_expected) {
$this
->expectException(BadRequestHttpException::class);
$this
->expectExceptionMessage("Invalid media library parameters specified.");
}
$state = MediaLibraryState::fromRequest(new Request($query));
$this
->assertInstanceOf(MediaLibraryState::class, $state);
}
public function testFromRequestQueryLess() {
$this
->expectException(\InvalidArgumentException::class);
$this
->expectExceptionMessage('The opener ID parameter is required and must be a string.');
$state = MediaLibraryState::fromRequest(new Request());
$this
->assertInstanceOf(MediaLibraryState::class, $state);
}
public function providerFromRequest() {
$test_data = [];
$test_data['valid parameters'] = [
[],
FALSE,
];
$test_data['changed with same values'] = [
[
'media_library_opener_id' => 'test',
'media_library_allowed_types' => [
'file',
'image',
],
'media_library_selected_type' => 'image',
'media_library_remaining' => 2,
],
FALSE,
];
$test_data['changed opener ID'] = [
[
'media_library_opener_id' => 'fail',
],
TRUE,
];
$test_data['changed allowed types'] = [
[
'media_library_allowed_types' => [
'audio',
'image',
],
],
TRUE,
];
$test_data['changed selected type'] = [
[
'media_library_selected_type' => 'file',
],
TRUE,
];
$test_data['changed remaining'] = [
[
'media_library_remaining' => 4,
],
TRUE,
];
$test_data['changed hash'] = [
[
'hash' => 'fail',
],
TRUE,
];
return $test_data;
}
public function testOpenerParameters() {
$state = MediaLibraryState::create('test', [
'file',
], 'file', -1, [
'foo' => 'baz',
]);
$this
->assertSame([
'foo' => 'baz',
], $state
->getOpenerParameters());
}
public function testHashUnaffectedByMediaTypeOrder() {
$state1 = MediaLibraryState::create('test', [
'file',
'image',
], 'image', 2);
$state2 = MediaLibraryState::create('test', [
'image',
'file',
], 'image', 2);
$this
->assertSame($state1
->getHash(), $state2
->getHash());
}
public function testHashUnaffectedByOpenerParamOrder() {
$state1 = MediaLibraryState::create('test', [
'file',
], 'file', -1, [
'foo' => 'baz',
'baz' => 'foo',
]);
$state2 = MediaLibraryState::create('test', [
'file',
], 'file', -1, [
'baz' => 'foo',
'foo' => 'baz',
]);
$this
->assertSame($state1
->getHash(), $state2
->getHash());
}
}