tao-test/app/taoMediaManager/test/integration/model/MediaManagerBrowserTest.php

138 lines
5.9 KiB
PHP

<?php
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2014-2018 (original work) Open Assessment Technologies SA;
*
*/
namespace oat\taoMediaManager\test\integration\model;
use oat\taoMediaManager\model\fileManagement\FlySystemManagement;
use oat\taoMediaManager\model\MediaSource;
use oat\taoMediaManager\model\MediaService;
use oat\generis\test\TestCase;
use tao_models_classes_FileNotFoundException;
class MediaManagerBrowserTest extends TestCase
{
private $rootClass = '';
public function setUp(): void
{
$this->rootClass = new \core_kernel_classes_Class('http://myFancyDomaine.com/myGreatCLassUriForBrowserTest');
}
public function tearDown(): void
{
(MediaService::singleton())->deleteClass($this->rootClass);
}
public function testGetDirectory()
{
$this->rootClass->setLabel('myRootClass');
$acceptableMime = [];
$depth = 1;
$mediaSource = $this->initializeMediaSource();
$directory = $mediaSource->getDirectory(\tao_helpers_Uri::encode($this->rootClass->getUri()), $acceptableMime, $depth);
$this->assertIsArray($directory, 'The result should be an array');
$this->assertArrayHasKey('label', $directory, 'The result should contain "label"');
$this->assertArrayHasKey('path', $directory, 'The result should contain "path"');
$this->assertArrayHasKey('children', $directory, 'The result should contain "children"');
$this->assertIsArray($directory['children'], 'Children should be an array');
$this->assertEquals('myRootClass', $directory['label'], 'The label is not correct');
$this->assertEquals('taomedia://mediamanager/' . \tao_helpers_Uri::encode($this->rootClass->getUri()), $directory['path'], 'The path is not correct');
$this->rootClass->createSubClass('mySubClass1');
$this->rootClass->createSubClass('mySubClass0');
$newDirectory = $mediaSource->getDirectory(\tao_helpers_Uri::encode($this->rootClass->getUri()), $acceptableMime, $depth);
$this->assertIsArray($newDirectory['children'], 'Children should be an array');
$this->assertNotEmpty($newDirectory['children'], 'Children should not be empty');
$labels = [];
foreach ($newDirectory['children'] as $i => $child) {
$this->assertIsArray($child, 'The result should be an array');
if (isset($child['parent'])) {
$this->assertArrayHasKey('label', $child, 'The result should contain "label"');
$this->assertArrayHasKey('path', $child, 'The result should contain "path"');
$labels[$child['label']] = $child['label'];
} else {
$this->assertArrayHasKey('name', $child, 'The result should contain "name"');
$this->assertArrayHasKey('uri', $child, 'The result should contain "uri"');
$this->assertArrayHasKey('link', $child, 'The result should contain "link"');
}
}
$this->assertEquals(2, count($labels));
$this->assertContains('mySubClass0', $labels);
$this->assertContains('mySubClass1', $labels);
}
public function testGetFileInfo()
{
$instance = $this->rootClass->createInstance('Brazil.png');
$instance->setPropertyValue(new \core_kernel_classes_Property(MediaService::PROPERTY_LINK), 'myGreatLink');
$instance->setPropertyValue(new \core_kernel_classes_Property(MediaService::PROPERTY_MIME_TYPE), 'image/png');
$uri = $instance->getUri();
$fileInfo = $this->initializeMediaSource()->getFileInfo(\tao_helpers_Uri::decode($uri));
$this->assertIsArray($fileInfo, 'The result should be an array');
$this->assertArrayHasKey('name', $fileInfo, 'The result should contain "name"');
$this->assertArrayHasKey('mime', $fileInfo, 'The result should contain "mime"');
$this->assertArrayHasKey('size', $fileInfo, 'The result should contain "size"');
$this->assertArrayHasKey('uri', $fileInfo, 'The result should contain "size"');
$this->assertEquals($instance->getLabel(), $fileInfo['name'], 'The file name is not correct');
$this->assertEquals('image/png', $fileInfo['mime'], 'The mime type is not correct');
$this->assertEquals('taomedia://mediamanager/' . \tao_helpers_Uri::encode($uri), $fileInfo['uri'], 'The uri is not correct');
}
public function testGetFileInfoFail()
{
$this->expectException(tao_models_classes_FileNotFoundException::class);
$this->expectExceptionMessage('File A Fake link not found');
$link = 'A Fake link';
$this->initializeMediaSource()->getFileInfo($link);
}
private function initializeMediaSource()
{
$fileManagerMock = $this->getMockBuilder(FlySystemManagement::class)
->setMethods(['getFileSize', 'deleteFile'])
->getMock();
$fileManagerMock->expects($this->any())
->method('getFileSize')
->willReturn(100);
$mediaSource = new MediaSource(['lang' => 'EN_en', 'rootClass' => $this->rootClass]);
$ref = new \ReflectionProperty(MediaSource::class, 'fileManagementService');
$ref->setAccessible(true);
$ref->setValue($mediaSource, $fileManagerMock);
return $mediaSource;
}
}