2021-02-16 15:08:15 +01:00
< ? php
2021-08-10 09:55:15 +02:00
declare ( strict_types = 1 );
2021-02-16 15:08:15 +01:00
namespace WerkraumMedia\ThueCat\Tests\Unit\Domain\Model\Frontend ;
/*
* Copyright ( C ) 2021 Daniel Siepmann < coding @ daniel - siepmann . de >
*
* 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 ; either version 2
* 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 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 .
*/
2023-11-30 13:52:23 +01:00
use PHPUnit\Framework\Attributes\Test ;
2021-02-16 15:08:15 +01:00
use PHPUnit\Framework\TestCase ;
2023-08-29 13:54:16 +02:00
use TYPO3\CMS\Core\Resource\FileReference ;
2021-02-16 15:08:15 +01:00
use WerkraumMedia\ThueCat\Domain\Model\Frontend\Media ;
class MediaTest extends TestCase
{
2023-11-30 13:52:23 +01:00
#[Test]
2021-02-16 15:08:15 +01:00
public function canBeCreated () : void
{
$subject = new Media ( '[]' );
self :: assertInstanceOf ( Media :: class , $subject );
}
2023-11-30 13:52:23 +01:00
#[Test]
2021-02-16 15:08:15 +01:00
public function returnsMainImageIfPresent () : void
{
$subject = new Media ( '[{"mainImage":false,"type":"image","title":"Erfurt-Dom-und-Severikirche.jpg"},{"mainImage":true,"type":"image","title":"Erfurt-Dom und Severikirche-beleuchtet.jpg"}]' );
self :: assertSame ([
'mainImage' => true ,
'type' => 'image' ,
'title' => 'Erfurt-Dom und Severikirche-beleuchtet.jpg' ,
], $subject -> getMainImage ());
}
2023-11-30 13:52:23 +01:00
#[Test]
2021-02-16 15:08:15 +01:00
public function returnsEmptyArrayAsMainImageFallback () : void
{
$subject = new Media ( '[]' );
self :: assertSame ([], $subject -> getMainImage ());
}
2023-11-30 13:52:23 +01:00
#[Test]
2021-02-16 15:08:15 +01:00
public function returnsImagesAsArray () : void
{
$subject = new Media ( '[{"mainImage":false,"type":"image","title":"Erfurt-Dom-und-Severikirche.jpg"},{"mainImage":true,"type":"image","title":"Erfurt-Dom und Severikirche-beleuchtet.jpg"}]' );
self :: assertSame ([
[
'mainImage' => false ,
'type' => 'image' ,
'title' => 'Erfurt-Dom-und-Severikirche.jpg' ,
],
[
'mainImage' => true ,
'type' => 'image' ,
'title' => 'Erfurt-Dom und Severikirche-beleuchtet.jpg' ,
],
], $subject -> getImages ());
}
2022-11-29 11:56:53 +01:00
2023-11-30 13:52:23 +01:00
#[Test]
2022-11-29 11:56:53 +01:00
public function returnsExtraImagesAsArray () : void
{
$subject = new Media ( '[{"mainImage":false,"type":"image","title":"Erfurt-Dom-und-Severikirche.jpg"},{"mainImage":true,"type":"image","title":"Erfurt-Dom und Severikirche-beleuchtet.jpg"}]' );
self :: assertSame ([
[
'mainImage' => false ,
'type' => 'image' ,
'title' => 'Erfurt-Dom-und-Severikirche.jpg' ,
],
], $subject -> getExtraImages ());
}
2023-01-04 07:33:48 +01:00
2023-11-30 13:52:23 +01:00
#[Test]
2023-01-04 07:33:48 +01:00
public function doesNotAddCopyrightAuthorIfItDoesntExist () : void
{
$subject = new Media ( json_encode ([
[
'mainImage' => false ,
'type' => 'image' ,
'title' => 'Erfurt-Dom-und-Severikirche.jpg' ,
],
[
'mainImage' => false ,
'type' => 'image' ,
'title' => 'Erfurt-Dom und Severikirche-beleuchtet.jpg' ,
],
]) ? : '' );
self :: assertArrayNotHasKey (
'copyrightAuthor' ,
$subject -> getExtraImages ()[ 0 ]
);
self :: assertArrayNotHasKey (
'copyrightAuthor' ,
$subject -> getExtraImages ()[ 1 ]
);
}
2023-11-30 13:52:23 +01:00
#[Test]
2023-01-04 07:33:48 +01:00
public function addsCopyrightAuthorFromLicenseAuthor () : void
{
$subject = new Media ( json_encode ([
[
'mainImage' => false ,
'type' => 'image' ,
'title' => 'Erfurt-Dom-und-Severikirche.jpg' ,
'license' => [
'author' => 'Full Name 1' ,
],
],
[
'mainImage' => false ,
'type' => 'image' ,
'title' => 'Erfurt-Dom und Severikirche-beleuchtet.jpg' ,
'license' => [
'author' => 'Full Name 2' ,
],
],
]) ? : '' );
self :: assertSame (
'Full Name 1' ,
$subject -> getExtraImages ()[ 0 ][ 'copyrightAuthor' ]
);
self :: assertSame (
'Full Name 2' ,
$subject -> getExtraImages ()[ 1 ][ 'copyrightAuthor' ]
);
}
2023-11-30 13:52:23 +01:00
#[Test]
2023-01-04 07:33:48 +01:00
public function addsCopyrightAuthorFromAuthor () : void
{
$subject = new Media ( json_encode ([
[
'mainImage' => false ,
'type' => 'image' ,
'title' => 'Erfurt-Dom-und-Severikirche.jpg' ,
'author' => 'Full Name 1' ,
],
[
'mainImage' => false ,
'type' => 'image' ,
'title' => 'Erfurt-Dom und Severikirche-beleuchtet.jpg' ,
'author' => 'Full Name 2' ,
],
]) ? : '' );
self :: assertSame (
'Full Name 1' ,
$subject -> getExtraImages ()[ 0 ][ 'copyrightAuthor' ]
);
self :: assertSame (
'Full Name 2' ,
$subject -> getExtraImages ()[ 1 ][ 'copyrightAuthor' ]
);
}
2023-11-30 13:52:23 +01:00
#[Test]
2023-01-04 07:33:48 +01:00
public function addsCopyrightAuthorFromAuthorWithHigherPrio () : void
{
$subject = new Media ( json_encode ([
[
'mainImage' => false ,
'type' => 'image' ,
'title' => 'Erfurt-Dom-und-Severikirche.jpg' ,
'author' => 'Full Name 1' ,
'license' => [
'author' => 'Full Name 1 license' ,
],
],
[
'mainImage' => false ,
'type' => 'image' ,
'title' => 'Erfurt-Dom und Severikirche-beleuchtet.jpg' ,
'author' => 'Full Name 2' ,
'license' => [
'author' => 'Full Name 2 license' ,
],
],
]) ? : '' );
self :: assertSame (
'Full Name 1' ,
$subject -> getExtraImages ()[ 0 ][ 'copyrightAuthor' ]
);
self :: assertSame (
'Full Name 2' ,
$subject -> getExtraImages ()[ 1 ][ 'copyrightAuthor' ]
);
}
2023-08-29 13:54:16 +02:00
2023-11-30 13:52:23 +01:00
#[Test]
2023-08-29 13:54:16 +02:00
public function returnsEmptyArrayAsDefaultForEditorialImages () : void
{
$subject = new Media ( '' );
self :: assertSame (
[],
$subject -> getEditorialImages ()
);
}
2023-11-30 13:52:23 +01:00
#[Test]
2023-08-29 13:54:16 +02:00
public function returnsSetEditorialImages () : void
{
$subject = new Media ( '' );
$reference1 = $this -> createStub ( FileReference :: class );
$reference2 = $this -> createStub ( FileReference :: class );
$subject -> setEditorialImages ([
$reference1 ,
$reference2 ,
]);
$images = $subject -> getEditorialImages ();
self :: assertCount ( 2 , $images );
self :: assertSame ( $reference1 , $images [ 0 ]);
self :: assertSame ( $reference2 , $images [ 1 ]);
}
2023-11-30 13:52:23 +01:00
#[Test]
2023-08-29 13:54:16 +02:00
public function returnsEmptyArrayAsDefaultForAllImages () : void
{
$subject = new Media ( '' );
self :: assertSame (
[],
$subject -> getAllImages ()
);
}
2023-11-30 13:52:23 +01:00
#[Test]
2023-08-29 13:54:16 +02:00
public function returnsAllImages () : void
{
$subject = new Media ( json_encode ([
[
'mainImage' => false ,
'type' => 'image' ,
'title' => 'Erfurt-Dom-und-Severikirche.jpg' ,
'author' => 'Full Name 1' ,
'license' => [
'author' => 'Full Name 1 license' ,
],
],
[
'mainImage' => false ,
'type' => 'image' ,
'title' => 'Erfurt-Dom und Severikirche-beleuchtet.jpg' ,
'author' => 'Full Name 2' ,
'license' => [
'author' => 'Full Name 2 license' ,
],
],
]) ? : '' );
$reference1 = $this -> createStub ( FileReference :: class );
$reference2 = $this -> createStub ( FileReference :: class );
$subject -> setEditorialImages ([
$reference1 ,
$reference2 ,
]);
self :: assertSame (
$reference1 ,
$subject -> getAllImages ()[ 0 ]
);
self :: assertSame (
$reference2 ,
$subject -> getAllImages ()[ 1 ]
);
self :: assertSame (
'Full Name 1' ,
$subject -> getAllImages ()[ 2 ][ 'copyrightAuthor' ]
);
self :: assertSame (
'Full Name 2' ,
$subject -> getAllImages ()[ 3 ][ 'copyrightAuthor' ]
);
}
2021-02-16 15:08:15 +01:00
}