Source of file SortableMenuRenderingTest.php
Size: 5,101 Bytes - Last Modified: 2021-12-23T10:21:02+00:00
/var/www/docs.ssmods.com/process/src/tests/SortableMenuRenderingTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 | <?php namespace Symbiote\SortableMenu\Tests; use Page; use SilverStripe\Core\Config\Config; use Symbiote\SortableMenu\SortableMenuExtension; use SilverStripe\Dev\FunctionalTest; class SortableMenuRenderingTest extends FunctionalTest { protected static $use_draft_site = true; protected $requireDefaultRecordsFrom = [ Page::class, ]; protected $usesDatabase = true; public static function setUpBeforeClass() { parent::setUpBeforeClass(); // NOTE(Jake): 2018-08-13 // // Add configs to SortableMenuExtension, then apply to // `Page` record. Because this modifies the DB fields, we need // to call `static::resetDBSchema(true, true);` // Config::modify()->set(SortableMenuExtension::class, 'menus', array( 'ShowInFooter' => array( 'Title' => 'Footer', ), 'ShowInSidebar' => array( 'Title' => 'Sidebar', ), )); Page::add_extension(SortableMenuExtension::class); static::resetDBSchema(true, true); } public function testRenderingMenusByOrder() { // Create footer items $record = new Page(); $record->Title = 'Footer Menu Item #3'; $record->ShowInFooter = true; $record->SortShowInFooter = 3; $record->write(); $record = new Page(); $record->Title = 'Footer Menu Item #1'; $record->ShowInFooter = true; $record->SortShowInFooter = 1; $record->write(); $record = new Page(); $record->Title = 'Footer Menu Item #2'; $record->ShowInFooter = true; $record->SortShowInFooter = 2; $record->write(); // Get a page from DB - So we can call $SortableMenus() in the template. $record = Page::get()->filter(array('SortShowInFooter' => 1))->first(); $this->assertNotNull($record); $expectedHTML = <<<HTML <div class="footer"> <ul class="footer-nav"> <li> <a href="/footer-menu-item-1/"> Footer Menu Item #1 </a> </li> <li> <a href="/footer-menu-item-2/"> Footer Menu Item #2 </a> </li> <li> <a href="/footer-menu-item-3/"> Footer Menu Item #3 </a> </li> </ul> </div> HTML; $actualHTML = $record->renderWith([['type' => 'Includes', 'TestRenderingMenusByOrder']])->forTemplate(); $this->assertEqualIgnoringWhitespace($expectedHTML, $actualHTML); } public function testPassInMenuList() { // Create footer items $record = new Page(); $record->Title = 'Sidebar Menu Item #3'; $record->ShowInSidebar = true; $record->SortShowInSidebar = 3; $record->write(); $record = new Page(); $record->Title = 'Sidebar Menu Item #1'; $record->ShowInSidebar = true; $record->SortShowInSidebar = 1; $record->write(); $record = new Page(); $record->Title = 'Sidebar Menu Item #2'; $record->ShowInSidebar = true; $record->SortShowInSidebar = 2; $record->write(); // Get a page from DB - So we can call $SortableMenus() in the template. $record = Page::get()->filter(array('SortShowInSidebar' => 1))->first(); $this->assertNotNull($record); // Test that it contains `SortableMenuFieldName` fiel $sortableMenus = $record->SortableMenu('ShowInSidebar'); $this->assertEquals( 'ShowInSidebar', $sortableMenus->SortableMenuFieldName, 'SortableMenuFieldName should map to the DB field name (ie. ShowInSidebar) so that when you pass a menu down across templates, you\'re still able to cache it with a <% cache %> block' ); // Test that it has expected menu count $this->assertEquals(3, $sortableMenus->count()); // Make sure using $SortableMenuFieldName with cache blocks works as expected $expectedHTML = <<<HTML <div class="TestPassInMenuList"> <ul class="navigation"> <li> <a href="/sidebar-menu-item-1/"> Sidebar Menu Item #1 </a> </li> <li> <a href="/sidebar-menu-item-2/"> Sidebar Menu Item #2 </a> </li> <li> <a href="/sidebar-menu-item-3/"> Sidebar Menu Item #3 </a> </li> </ul> </div> HTML; $actualHTML = $record->customise(array( 'PassedInMenuList' => $sortableMenus, ))->renderWith([['type' => 'Includes', 'TestPassInMenuList']])->forTemplate(); $this->assertEqualIgnoringWhitespace($expectedHTML, $actualHTML); } /** * Taken from "framework\tests\view\SSViewerTest.php" */ private function assertEqualIgnoringWhitespace($a, $b, $message = '') { $this->assertEquals(preg_replace('/\s/', '', $a), preg_replace('/\s/', '', $b), $message); } } |