Source of file FilesystemPublisherTest.php
Size: 12,690 Bytes - Last Modified: 2021-12-23T10:47:02+00:00
/var/www/docs.ssmods.com/process/src/tests/php/Publisher/FilesystemPublisherTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 | <?php namespace SilverStripe\StaticPublishQueue\Test\Publisher; use SilverStripe\Assets\Filesystem; use SilverStripe\CMS\Model\RedirectorPage; use SilverStripe\CMS\Model\SiteTree; use SilverStripe\Control\Director; use SilverStripe\Control\HTTPApplication; use SilverStripe\Core\Config\Config; use SilverStripe\Dev\SapphireTest; use SilverStripe\Dev\TestKernel; use SilverStripe\StaticPublishQueue\Extension\Publishable\PublishableSiteTree; use SilverStripe\StaticPublishQueue\Publisher\FilesystemPublisher; use SilverStripe\StaticPublishQueue\Test\StaticPublisherTest\Model\StaticPublisherTestPage; use SilverStripe\View\SSViewer; use Symbiote\QueuedJobs\Services\QueuedJobService; /** * Tests for the {@link FilesystemPublisher} class. * * @package staticpublishqueue */ class FilesystemPublisherTest extends SapphireTest { protected $usesDatabase = true; protected static $required_extensions = [ SiteTree::class => [ PublishableSiteTree::class, ], ]; /** * @var FilesystemPublisher */ private $fsp = null; protected function setUp() { parent::setUp(); Config::modify()->set(FilesystemPublisher::class, 'domain_based_caching', false); Config::modify()->set(QueuedJobService::class, 'use_shutdown_function', false); Config::modify()->set(Director::class, 'alternate_base_url', 'http://example.com/'); $mockFSP = $this->getMockBuilder(FilesystemPublisher::class)->setMethods([ 'getHTTPApplication', ])->getMock(); $mockFSP->method('getHTTPApplication')->willReturnCallback(function () { return new HTTPApplication(new TestKernel(BASE_PATH)); }); $this->fsp = $mockFSP->setDestFolder('cache/testing/'); } protected function tearDown() { if ($this->fsp !== null && file_exists($this->fsp->getDestPath())) { Filesystem::removeFolder($this->fsp->getDestPath()); } parent::tearDown(); } public function testUrlToPathWithRelativeUrls() { $reflection = new \ReflectionClass(FilesystemPublisher::class); $urlToPath = $reflection->getMethod('URLtoPath'); $urlToPath->setAccessible(true); $this->assertSame( 'index', $urlToPath->invokeArgs($this->fsp, ['/']) ); $this->assertSame( 'about-us', $urlToPath->invokeArgs($this->fsp, ['about-us']) ); $this->assertSame( 'parent/child', $urlToPath->invokeArgs($this->fsp, ['parent/child']) ); } public function testUrlToPathWithAbsoluteUrls() { $reflection = new \ReflectionClass(FilesystemPublisher::class); $urlToPath = $reflection->getMethod('URLtoPath'); $urlToPath->setAccessible(true); $url = Director::absoluteBaseUrl(); $this->assertSame( 'index', $urlToPath->invokeArgs($this->fsp, [$url]) ); $url = Director::absoluteBaseUrl() . 'about-us'; $this->assertSame( 'about-us', $urlToPath->invokeArgs($this->fsp, [$url]) ); $url = Director::absoluteBaseUrl() . 'parent/child'; $this->assertSame( 'parent/child', $urlToPath->invokeArgs($this->fsp, [$url]) ); } public function testUrlToPathWithDomainBasedCaching() { Config::modify()->set(FilesystemPublisher::class, 'domain_based_caching', true); $reflection = new \ReflectionClass(FilesystemPublisher::class); $urlToPath = $reflection->getMethod('URLtoPath'); $urlToPath->setAccessible(true); $this->fsp->setFileExtension('html'); $url = 'http://domain1.com/'; $this->assertSame( 'domain1.com/index', $urlToPath->invokeArgs($this->fsp, [$url]) ); $url = 'http://domain1.com/about-us'; $this->assertSame( 'domain1.com/about-us', $urlToPath->invokeArgs($this->fsp, [$url]) ); $url = 'http://domain2.com/parent/child'; $this->assertSame( 'domain2.com/parent/child', $urlToPath->invokeArgs($this->fsp, [$url]) ); } public function testMenu2LinkingMode() { SSViewer::set_themes(null); $reflection = new \ReflectionClass(FilesystemPublisher::class); $urlToPath = $reflection->getMethod('URLtoPath'); $urlToPath->setAccessible(true); $level1 = new StaticPublisherTestPage(); $level1->URLSegment = 'test-level-1'; $level1->write(); $level1->publishRecursive(); $level2_1 = new StaticPublisherTestPage(); $level2_1->URLSegment = 'test-level-2-1'; $level2_1->ParentID = $level1->ID; $level2_1->write(); $level2_1->publishRecursive(); $this->fsp->publishURL($level1->Link(), true); $this->fsp->publishURL($level2_1->Link(), true); $static2_1FilePath = $this->fsp->getDestPath() . $urlToPath->invokeArgs($this->fsp, [$level2_1->Link()]); $this->assertFileExists($static2_1FilePath . '.html'); $this->assertFileExists($static2_1FilePath . '.php'); $this->assertContains( 'current', file_get_contents($static2_1FilePath . '.html') ); $level2_2 = new StaticPublisherTestPage(); $level2_2->URLSegment = 'test-level-2-2'; $level2_2->ParentID = $level1->ID; $level2_2->write(); $level2_2->publishRecursive(); $this->fsp->publishURL($level2_2->Link(), true); $static2_2FilePath = $this->fsp->getDestPath() . $urlToPath->invokeArgs($this->fsp, [$level2_2->Link()]); $this->assertFileExists($static2_2FilePath . '.html'); $this->assertFileExists($static2_2FilePath . '.php'); $this->assertContains( 'linkcurrent', file_get_contents($static2_2FilePath . '.html') ); } public function testOnlyHTML() { $this->fsp->setFileExtension('html'); $level1 = new StaticPublisherTestPage(); $level1->URLSegment = 'mimetype'; $level1->write(); $level1->publishRecursive(); $this->fsp->publishURL($level1->Link(), true); $staticFilePath = $this->fsp->getDestPath() . 'mimetype'; $this->assertFileExists($staticFilePath . '.html'); $this->assertFileNotExists($staticFilePath . '.php'); $this->assertSame( '<div class="statically-published" style="display: none"></div>', trim(file_get_contents($staticFilePath . '.html')) ); } public function testPurgeURL() { $level1 = new StaticPublisherTestPage(); $level1->URLSegment = 'to-be-purged'; $level1->write(); $level1->publishRecursive(); $this->fsp->publishURL('to-be-purged', true); $this->assertFileExists($this->fsp->getDestPath() . 'to-be-purged.html'); $this->assertFileExists($this->fsp->getDestPath() . 'to-be-purged.php'); $this->fsp->purgeURL('to-be-purged'); $this->assertFileNotExists($this->fsp->getDestPath() . 'to-be-purged.html'); $this->assertFileNotExists($this->fsp->getDestPath() . 'to-be-purged.php'); } public function testPurgeURLAfterSwitchingExtensions() { $level1 = new StaticPublisherTestPage(); $level1->URLSegment = 'purge-me'; $level1->write(); $level1->publishRecursive(); $this->fsp->publishURL('purge-me', true); $this->assertFileExists($this->fsp->getDestPath() . 'purge-me.html'); $this->assertFileExists($this->fsp->getDestPath() . 'purge-me.php'); $this->fsp->setFileExtension('html'); $this->fsp->purgeURL('purge-me'); $this->assertFileNotExists($this->fsp->getDestPath() . 'purge-me.html'); $this->assertFileNotExists($this->fsp->getDestPath() . 'purge-me.php'); } public function testNoErrorPagesWhenHTMLOnly() { $this->fsp->setFileExtension('html'); $this->fsp->publishURL('not_really_there', true); $this->assertFileNotExists($this->fsp->getDestPath() . 'not_really_there.html'); $this->assertFileNotExists($this->fsp->getDestPath() . 'not_really_there.php'); } public function testErrorPageWhenPHP() { $this->fsp->publishURL('not_really_there', true); $this->assertFileExists($this->fsp->getDestPath() . 'not_really_there.html'); $this->assertFileExists($this->fsp->getDestPath() . 'not_really_there.php'); $phpCacheConfig = require $this->fsp->getDestPath() . 'not_really_there.php'; $this->assertSame(404, $phpCacheConfig['responseCode']); } public function testRedirectorPageWhenPHP() { $redirectorPage = RedirectorPage::create(); $redirectorPage->URLSegment = 'somewhere-else'; $redirectorPage->RedirectionType = 'External'; $redirectorPage->ExternalURL = 'silverstripe.org'; $redirectorPage->write(); $redirectorPage->publishRecursive(); $this->fsp->publishURL('somewhere-else', true); $this->assertFileExists($this->fsp->getDestPath() . 'somewhere-else.html'); $this->assertContains( 'Click this link if your browser does not redirect you', file_get_contents($this->fsp->getDestPath() . 'somewhere-else.html') ); $this->assertFileExists($this->fsp->getDestPath() . 'somewhere-else.php'); $phpCacheConfig = require $this->fsp->getDestPath() . 'somewhere-else.php'; $this->assertSame(301, $phpCacheConfig['responseCode']); $this->assertContains('location: http://silverstripe.org', $phpCacheConfig['headers']); } public function testRedirectorPageWhenHTMLOnly() { $this->fsp->setFileExtension('html'); $redirectorPage = RedirectorPage::create(); $redirectorPage->URLSegment = 'somewhere-else'; $redirectorPage->RedirectionType = 'External'; $redirectorPage->ExternalURL = 'silverstripe.org'; $redirectorPage->write(); $redirectorPage->publishRecursive(); $this->fsp->publishURL('somewhere-else', true); $this->assertFileExists($this->fsp->getDestPath() . 'somewhere-else.html'); $this->assertContains( 'Click this link if your browser does not redirect you', file_get_contents($this->fsp->getDestPath() . 'somewhere-else.html') ); $this->assertFileNotExists($this->fsp->getDestPath() . 'somewhere-else.php'); } /** * @dataProvider providePathsToURL */ public function testPathToURL($expected, $path) { $reflection = new \ReflectionClass(FilesystemPublisher::class); $pathToURL = $reflection->getMethod('pathToURL'); $pathToURL->setAccessible(true); $this->assertSame( $expected, $pathToURL->invoke($this->fsp, $this->fsp->getDestPath() . $path) ); } public function providePathsToURL() { return [ ['http://example.com/', 'index.html'], ['http://example.com/about-us', 'about-us.html'], ['http://example.com/about-us', 'about-us.php'], ['http://example.com/parent/child', 'parent/child.html'], ]; } public function testGetPublishedURLs() { $level1 = new StaticPublisherTestPage(); $level1->URLSegment = 'find-me'; $level1->write(); $level1->publishRecursive(); $this->fsp->publishURL('find-me', true); // We have to redeclare this config because the testkernel wipes it when we generate the page response Director::config()->set('alternate_base_url', 'http://example.com'); $this->assertSame(['http://example.com/find-me'], $this->fsp->getPublishedURLs()); $level2_1 = new StaticPublisherTestPage(); $level2_1->URLSegment = 'find-me-child'; $level2_1->ParentID = $level1->ID; $level2_1->write(); $level2_1->publishRecursive(); $this->fsp->publishURL($level2_1->Link(), true); Director::config()->set('alternate_base_url', 'http://example.com'); $urls = $this->fsp->getPublishedURLs(); $this->assertContains('http://example.com/find-me', $urls); $this->assertContains('http://example.com/find-me/find-me-child', $urls); $this->assertCount(2, $urls); $this->fsp->purgeURL('find-me'); $this->assertSame(['http://example.com/find-me/find-me-child'], $this->fsp->getPublishedURLs()); } } |