|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\PageCache\Test\Unit\Model\System\Config\Backend; |
| 10 | + |
| 11 | +use Magento\PageCache\Model\System\Config\Backend\Ttl; |
| 12 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 13 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 14 | +use Magento\Framework\Escaper; |
| 15 | +use Magento\Framework\Exception\LocalizedException; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | + |
| 18 | +/** |
| 19 | + * Class for tesing backend model for processing Public content cache lifetime settings. |
| 20 | + */ |
| 21 | +class TtlTest extends TestCase |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var Ttl |
| 25 | + */ |
| 26 | + private $ttl; |
| 27 | + |
| 28 | + /* |
| 29 | + * @var \Magento\Framework\Escaper|\PHPUnit_Framework_MockObject_MockObject |
| 30 | + */ |
| 31 | + private $escaperMock; |
| 32 | + |
| 33 | + /** |
| 34 | + * @inheritDoc |
| 35 | + */ |
| 36 | + protected function setUp() |
| 37 | + { |
| 38 | + $objectManager = new ObjectManager($this); |
| 39 | + $configMock = $this->getMockForAbstractClass(ScopeConfigInterface::class); |
| 40 | + $configMock->expects($this->any()) |
| 41 | + ->method('getValue') |
| 42 | + ->with('system/full_page_cache/default') |
| 43 | + ->willReturn(['ttl' => 86400]); |
| 44 | + |
| 45 | + $this->escaperMock = $this->getMockBuilder(Escaper::class)->disableOriginalConstructor()->getMock(); |
| 46 | + |
| 47 | + $this->ttl = $objectManager->getObject( |
| 48 | + Ttl::class, |
| 49 | + [ |
| 50 | + 'config' => $configMock, |
| 51 | + 'data' => ['field' => 'ttl'], |
| 52 | + 'escaper' => $this->escaperMock, |
| 53 | + ] |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @return array |
| 59 | + */ |
| 60 | + public function getValidValues(): array |
| 61 | + { |
| 62 | + return [ |
| 63 | + ['3600', '3600'], |
| 64 | + ['10000', '10000'], |
| 65 | + ['100000', '100000'], |
| 66 | + ['1000000', '1000000'], |
| 67 | + ]; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @param string $value |
| 72 | + * @param string $expectedValue |
| 73 | + * @return void |
| 74 | + * @dataProvider getValidValues |
| 75 | + */ |
| 76 | + public function testBeforeSave(string $value, string $expectedValue) |
| 77 | + { |
| 78 | + $this->ttl->setValue($value); |
| 79 | + $this->ttl->beforeSave(); |
| 80 | + $this->assertEquals($expectedValue, $this->ttl->getValue()); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @return array |
| 85 | + */ |
| 86 | + public function getInvalidValues(): array |
| 87 | + { |
| 88 | + return [ |
| 89 | + ['<script>alert(1)</script>'], |
| 90 | + ['apple'], |
| 91 | + ['123 street'], |
| 92 | + ['-123'], |
| 93 | + ]; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @param string $value |
| 98 | + * @return void |
| 99 | + * @expectedException \Magento\Framework\Exception\LocalizedException |
| 100 | + * @expectedExceptionMessageRegExp /Ttl value ".+" is not valid. Please .+ only numbers equal or greater than zero./ |
| 101 | + * @dataProvider getInvalidValues |
| 102 | + */ |
| 103 | + public function testBeforeSaveInvalid(string $value) |
| 104 | + { |
| 105 | + $this->ttl->setValue($value); |
| 106 | + $this->escaperMock->expects($this->any())->method('escapeHtml')->with($value)->willReturn($value); |
| 107 | + $this->ttl->beforeSave(); |
| 108 | + } |
| 109 | +} |
0 commit comments