Files

191 lines
7.2 KiB
PHP

<?php
/**
* @package Plumrocket_CookieConsent
* @copyright Copyright (c) 2022 Plumrocket Inc. (https://plumrocket.com)
* @license https://plumrocket.com/license End-user License Agreement
*/
declare(strict_types=1);
namespace Plumrocket\CookieConsent\Test\Unit\Model\Integration;
use Magento\Framework\Exception\NoSuchEntityException;
use PHPUnit\Framework\TestCase;
use Plumrocket\Base\Model\Utils\Config;
use Plumrocket\CookieConsent\Api\Data\CategoryInterface;
use Plumrocket\CookieConsent\Model\CategoryRepository;
use Plumrocket\CookieConsent\Model\Integration\NoCookieYoutube;
/**
* @since 1.9.0
*/
class NoCookieYoutubeTest extends TestCase
{
/**
* @var \Plumrocket\CookieConsent\Api\IsUserOptInInterface
*/
private $noCookieYoutube;
/**
* @var \PHPUnit\Framework\MockObject\MockObject|\Plumrocket\CookieConsent\Helper\Config
*/
private $configMock;
/**
* @var \PHPUnit\Framework\MockObject\MockObject|\Plumrocket\CookieConsent\Model\CategoryRepository
*/
private $categoryRepositoryMock;
/**
* @var \PHPUnit\Framework\MockObject\MockObject|\Plumrocket\Base\Model\Utils\Config
*/
private $configUtilsMock;
/**
* @var \PHPUnit\Framework\MockObject\MockObject|\Plumrocket\CookieConsent\Api\Data\CategoryInterface
*/
private $cookieCategoryMock;
protected function setUp(): void
{
$this->configMock = $this->createMock(\Plumrocket\CookieConsent\Helper\Config::class);
$this->categoryRepositoryMock = $this->createMock(CategoryRepository::class);
$this->configUtilsMock = $this->createMock(Config::class);
$this->cookieCategoryMock = $this->getMockForAbstractClass(CategoryInterface::class);
$this->noCookieYoutube = new NoCookieYoutube(
$this->configMock,
$this->categoryRepositoryMock,
$this->configUtilsMock
);
}
public function testDisabledExtension(): void
{
$this->configMock->method('isModuleEnabled')->willReturn(false);
$this->assertFalse($this->noCookieYoutube->isEnabled());
}
public function testNotConfiguredCategory(): void
{
$this->configMock->method('isModuleEnabled')->willReturn(true);
$this->configUtilsMock
->method('getStoreConfig')
->with(NoCookieYoutube::XML_PATH_CATEGORY_KEY)
->willReturn(null);
$this->assertFalse($this->noCookieYoutube->isEnabled());
}
public function testNotExistingCategory(): void
{
$this->configMock->method('isModuleEnabled')->willReturn(true);
$this->configUtilsMock
->method('getStoreConfig')
->with(NoCookieYoutube::XML_PATH_CATEGORY_KEY)
->willReturn('test');
$this->categoryRepositoryMock->method('get')->willThrowException(new NoSuchEntityException(__('')));
$this->assertFalse($this->noCookieYoutube->isEnabled());
}
public function testDisabledCategory(): void
{
$this->configMock->method('isModuleEnabled')->willReturn(true);
$this->configUtilsMock
->method('getStoreConfig')
->with(NoCookieYoutube::XML_PATH_CATEGORY_KEY)
->willReturn('test');
$this->cookieCategoryMock->method('isEnabled')->willReturn(false);
$this->categoryRepositoryMock->method('get')->willReturn($this->cookieCategoryMock);
$this->assertFalse($this->noCookieYoutube->isEnabled());
}
public function testEssentialCategory(): void
{
$this->configMock->method('isModuleEnabled')->willReturn(true);
$this->configUtilsMock
->method('getStoreConfig')
->with(NoCookieYoutube::XML_PATH_CATEGORY_KEY)
->willReturn('test');
$this->cookieCategoryMock->method('isEnabled')->willReturn(true);
$this->cookieCategoryMock->method('isEssential')->willReturn(true);
$this->categoryRepositoryMock->method('get')->willReturn($this->cookieCategoryMock);
$this->assertFalse($this->noCookieYoutube->isEnabled());
}
public function testCorrectSettingsCategory(): void
{
$this->configMock->method('isModuleEnabled')->willReturn(true);
$this->configUtilsMock
->method('getStoreConfig')
->with(NoCookieYoutube::XML_PATH_CATEGORY_KEY)
->willReturn('test');
$this->cookieCategoryMock->method('isEnabled')->willReturn(true);
$this->cookieCategoryMock->method('isEssential')->willReturn(false);
$this->categoryRepositoryMock->method('get')->willReturn($this->cookieCategoryMock);
$this->assertTrue($this->noCookieYoutube->isEnabled());
}
/**
* @dataProvider iframeHtml
*
* @param string $case
* @param string $html
* @param string $expectedHtml
* @return void
*/
public function testConvertHtml(string $case, string $html, string $expectedHtml): void
{
$this->configMock->method('isModuleEnabled')->willReturn(true);
$this->configUtilsMock
->method('getStoreConfig')
->with(NoCookieYoutube::XML_PATH_CATEGORY_KEY)
->willReturn('test');
$this->cookieCategoryMock->method('isEnabled')->willReturn(true);
$this->cookieCategoryMock->method('isEssential')->willReturn(false);
$this->categoryRepositoryMock->method('get')->willReturn($this->cookieCategoryMock);
$this->assertSame($expectedHtml, $this->noCookieYoutube->convertHtml($html), $case);
}
public function iframeHtml(): \Generator
{
yield [
'case' => 'Empty html',
'html' => '',
'expectedHtml' => '',
];
yield [
'case' => 'YouTube iframe html',
'html' => '<iframe width="560" height="315" src="https://www.youtube.com/embed/aaaaaaaaaaa" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>',
'expectedHtml' => '<iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/aaaaaaaaaaa" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>',
];
yield [
'case' => 'Not YouTube iframe html',
'html' => '<iframe width="560" height="315" src="https://www.test.com/embed/aaaaaaaaaaa"></iframe>',
'expectedHtml' => '<iframe width="560" height="315" src="https://www.test.com/embed/aaaaaaaaaaa"></iframe>',
];
yield [
'case' => 'YouTube iframe html',
'html' => <<<HTML
<iframe width="560" height="315"
src="https://www.youtube.com/embed/aaaaaaaaaaa"
title="YouTube video player"
frameborder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
</iframe>
HTML
,
'expectedHtml' => <<<HTML
<iframe width="560" height="315"
src="https://www.youtube-nocookie.com/embed/aaaaaaaaaaa"
title="YouTube video player"
frameborder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
</iframe>
HTML
];
}
}