*/ protected array $fixtures = ['core.Posts', 'core.Users']; /** * @var \Cake\View\View */ protected $View; /** * @var \TestApp\Controller\ViewPostsController */ protected $PostsController; /** * @var \TestApp\Controller\ThemePostsController */ protected $ThemePostsController; /** * @var \Cake\Controller\Controller */ protected $ThemeController; /** * setUp method */ public function setUp(): void { parent::setUp(); $request = new ServerRequest(); $this->PostsController = new ViewPostsController($request); $this->PostsController->index(); $this->View = $this->PostsController->createView(); $this->View->setTemplatePath('Posts'); $themeRequest = new ServerRequest(['url' => 'posts/index']); $this->ThemeController = new Controller($themeRequest); $this->ThemePostsController = new ThemePostsController($themeRequest); $this->ThemePostsController->index(); $this->loadPlugins(['TestPlugin', 'PluginJs', 'TestTheme', 'Company/TestPluginThree']); Configure::write('debug', true); } /** * tearDown method */ public function tearDown(): void { parent::tearDown(); $this->clearPlugins(); unset($this->View); unset($this->PostsController); unset($this->ThemePostsController); unset($this->ThemeController); } /** * Test getTemplateFileName method */ public function testGetTemplate(): void { $viewOptions = [ 'plugin' => null, 'name' => 'Pages', 'templatePath' => 'Pages', ]; $ThemeView = new TestView(null, null, null, $viewOptions); $ThemeView->setTheme('TestTheme'); $expected = TEST_APP . 'templates' . DS . 'Pages' . DS . 'home.php'; $result = $ThemeView->getTemplateFileName('home'); $this->assertPathEquals($expected, $result); $expected = Plugin::path('TestTheme') . 'templates' . DS . 'Posts' . DS . 'index.php'; $result = $ThemeView->getTemplateFileName('/Posts/index'); $this->assertPathEquals($expected, $result); $expected = Plugin::path('TestTheme') . 'templates' . DS . 'layout' . DS . 'default.php'; $result = $ThemeView->getLayoutFileName(); $this->assertPathEquals($expected, $result); $ThemeView->setLayoutPath('rss'); $expected = TEST_APP . 'templates' . DS . 'layout' . DS . 'rss' . DS . 'default.php'; $result = $ThemeView->getLayoutFileName(); $this->assertPathEquals($expected, $result); $ThemeView->setLayoutPath('email' . DS . 'html'); $expected = TEST_APP . 'templates' . DS . 'layout' . DS . 'email' . DS . 'html' . DS . 'default.php'; $result = $ThemeView->getLayoutFileName(); $this->assertPathEquals($expected, $result); $ThemeView = new TestView(null, null, null, $viewOptions); $ThemeView->setTheme('Company/TestPluginThree'); $expected = Plugin::path('Company/TestPluginThree') . 'templates' . DS . 'layout' . DS . 'default.php'; $result = $ThemeView->getLayoutFileName(); $this->assertPathEquals($expected, $result); } /** * Test getLayoutFileName method on plugin */ public function testPluginGetTemplate(): void { $viewOptions = [ 'plugin' => 'TestPlugin', 'name' => 'TestPlugin', 'templatePath' => 'Tests', 'template' => 'index', ]; $View = new TestView(null, null, null, $viewOptions); $expected = Plugin::path('TestPlugin') . 'templates' . DS . 'Tests' . DS . 'index.php'; $result = $View->getTemplateFileName('index'); $this->assertSame($expected, $result); $expected = Plugin::path('TestPlugin') . 'templates' . DS . 'layout' . DS . 'default.php'; $result = $View->getLayoutFileName(); $this->assertSame($expected, $result); } /** * Test that plugin files with absolute file paths are scoped * to the plugin and do now allow any file path. */ public function testPluginGetTemplateAbsoluteFail(): void { $this->expectException(MissingTemplateException::class); $viewOptions = [ 'plugin' => null, 'name' => 'Pages', 'viewPath' => 'Pages', ]; $view = new TestView(null, null, null, $viewOptions); $expected = TEST_APP . 'Plugin' . DS . 'Company' . DS . 'TestPluginThree' . DS . 'templates' . DS . 'Pages' . DS . 'index.php'; $result = $view->getTemplateFileName('Company/TestPluginThree./Pages/index'); $this->assertPathEquals($expected, $result); $view->getTemplateFileName('Company/TestPluginThree./etc/passwd'); } /** * Test getTemplateFileName method on plugin */ public function testPluginThemedGetTemplate(): void { $viewOptions = [ 'plugin' => 'TestPlugin', 'name' => 'TestPlugin', 'templatePath' => 'Tests', 'template' => 'index', 'theme' => 'TestTheme', ]; $ThemeView = new TestView(null, null, null, $viewOptions); $themePath = Plugin::path('TestTheme') . 'templates' . DS; $expected = $themePath . 'plugin' . DS . 'TestPlugin' . DS . 'Tests' . DS . 'index.php'; $result = $ThemeView->getTemplateFileName('index'); $this->assertPathEquals($expected, $result); $expected = $themePath . 'plugin' . DS . 'TestPlugin' . DS . 'layout' . DS . 'plugin_default.php'; $result = $ThemeView->getLayoutFileName('plugin_default'); $this->assertPathEquals($expected, $result); $expected = $themePath . 'layout' . DS . 'default.php'; $result = $ThemeView->getLayoutFileName('default'); $this->assertPathEquals($expected, $result); } /** * Test that plugin/$plugin_name is only appended to the paths it should be. */ public function testPathPluginGeneration(): void { $viewOptions = [ 'plugin' => 'TestPlugin', 'name' => 'TestPlugin', 'viewPath' => 'Tests', 'view' => 'index', ]; $View = new TestView(null, null, null, $viewOptions); $paths = $View->paths(); $expected = array_merge(App::path('templates'), App::core('templates')); $this->assertEquals($expected, $paths); $paths = $View->paths('TestPlugin'); $pluginPath = Plugin::path('TestPlugin'); $expected = [ TEST_APP . 'templates' . DS . 'plugin' . DS . 'TestPlugin' . DS, $pluginPath . 'templates' . DS, TEST_APP . 'templates' . DS, CORE_PATH . 'templates' . DS, ]; $this->assertPathEquals($expected, $paths); } /** * Test that themed plugin paths are generated correctly. */ public function testPathThemedPluginGeneration(): void { $viewOptions = [ 'plugin' => 'TestPlugin', 'name' => 'TestPlugin', 'viewPath' => 'Tests', 'view' => 'index', 'theme' => 'TestTheme', ]; $View = new TestView(null, null, null, $viewOptions); $paths = $View->paths('TestPlugin'); $pluginPath = Plugin::path('TestPlugin'); $themePath = Plugin::path('TestTheme'); $expected = [ $themePath . 'templates' . DS . 'plugin' . DS . 'TestPlugin' . DS, $themePath . 'templates' . DS, TEST_APP . 'templates' . DS . 'plugin' . DS . 'TestPlugin' . DS, $pluginPath . 'templates' . DS, TEST_APP . 'templates' . DS, CORE_PATH . 'templates' . DS, ]; $this->assertPathEquals($expected, $paths); } /** * Test that multiple paths can be used in App.paths.templates. */ public function testMultipleAppPaths(): void { $viewOptions = [ 'plugin' => 'TestPlugin', 'name' => 'TestPlugin', 'viewPath' => 'Tests', 'view' => 'index', 'theme' => 'TestTheme', ]; $paths = Configure::read('App.paths.templates'); $paths[] = Plugin::path('TestPlugin') . 'templates' . DS; Configure::write('App.paths.templates', $paths); $View = new TestView(null, null, null, $viewOptions); $paths = $View->paths('TestPlugin'); $pluginPath = Plugin::path('TestPlugin'); $themePath = Plugin::path('TestTheme'); $expected = [ $themePath . 'templates' . DS . 'plugin' . DS . 'TestPlugin' . DS, $themePath . 'templates' . DS, TEST_APP . 'templates' . DS . 'plugin' . DS . 'TestPlugin' . DS, $pluginPath . 'templates' . DS . 'plugin' . DS . 'TestPlugin' . DS, $pluginPath . 'templates' . DS, TEST_APP . 'templates' . DS, TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'templates' . DS, CORE_PATH . 'templates' . DS, ]; $this->assertPathEquals($expected, $paths); } /** * Test that CamelCase'd plugins still find their view files. */ public function testCamelCasePluginGetTemplate(): void { $viewOptions = [ 'plugin' => 'TestPlugin', 'name' => 'TestPlugin', 'templatePath' => 'Tests', 'template' => 'index', ]; $View = new TestView(null, null, null, $viewOptions); $pluginPath = Plugin::path('TestPlugin'); $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'templates' . DS . 'Tests' . DS . 'index.php'; $result = $View->getTemplateFileName('index'); $this->assertPathEquals($expected, $result); $expected = $pluginPath . 'templates' . DS . 'layout' . DS . 'default.php'; $result = $View->getLayoutFileName(); $this->assertPathEquals($expected, $result); } /** * Test getTemplateFileName method */ public function testGetViewFileNames(): void { $viewOptions = [ 'plugin' => null, 'name' => 'Pages', 'templatePath' => 'Pages', ]; $View = new TestView(null, null, null, $viewOptions); $expected = TEST_APP . 'templates' . DS . 'Pages' . DS . 'home.php'; $result = $View->getTemplateFileName('home'); $this->assertPathEquals($expected, $result); $expected = TEST_APP . 'templates' . DS . 'Posts' . DS . 'index.php'; $result = $View->getTemplateFileName('/Posts/index'); $this->assertPathEquals($expected, $result); $expected = TEST_APP . 'templates' . DS . 'Posts' . DS . 'index.php'; $result = $View->getTemplateFileName('../Posts/index'); $this->assertPathEquals($expected, $result); $expected = TEST_APP . 'templates' . DS . 'Pages' . DS . 'page.home.php'; $result = $View->getTemplateFileName('page.home'); $this->assertPathEquals($expected, $result, 'Should not ruin files with dots.'); $expected = TEST_APP . 'templates' . DS . 'Pages' . DS . 'home.php'; $result = $View->getTemplateFileName('TestPlugin.home'); $this->assertPathEquals($expected, $result, 'Plugin is missing the view, cascade to app.'); $View->setTemplatePath('Tests'); $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'templates' . DS . 'Tests' . DS . 'index.php'; $result = $View->getTemplateFileName('TestPlugin.index'); $this->assertPathEquals($expected, $result); } /** * Test that getTemplateFileName() protects against malicious directory traversal. */ public function testGetViewFileNameDirectoryTraversal(): void { $this->expectException(InvalidArgumentException::class); $viewOptions = [ 'plugin' => null, 'name' => 'Pages', 'templatePath' => 'Pages', ]; $view = new TestView(null, null, null, $viewOptions); $view->ext('.php'); $view->getTemplateFileName('../../../bootstrap'); } /** * Test getTemplateFileName doesn't re-apply existing subdirectories */ public function testGetViewFileNameSubDir(): void { $viewOptions = [ 'plugin' => null, 'name' => 'Posts', 'templatePath' => 'Posts/json', 'layoutPath' => 'json', ]; $view = new TestView(null, null, null, $viewOptions); $expected = TEST_APP . 'templates' . DS . 'Posts' . DS . 'json' . DS . 'index.php'; $result = $view->getTemplateFileName('index'); $this->assertPathEquals($expected, $result); $view->setSubDir('json'); $result = $view->getTemplateFileName('index'); $expected = TEST_APP . 'templates' . DS . 'Posts' . DS . 'json' . DS . 'index.php'; $this->assertPathEquals($expected, $result); } /** * Test getTemplateFileName applies subdirectories on equal length names */ public function testGetViewFileNameSubDirLength(): void { $viewOptions = [ 'plugin' => null, 'name' => 'Jobs', 'templatePath' => 'Jobs', 'layoutPath' => 'json', ]; $view = new TestView(null, null, null, $viewOptions); $view->setSubDir('json'); $result = $view->getTemplateFileName('index'); $expected = TEST_APP . 'templates' . DS . 'Jobs' . DS . 'json' . DS . 'index.php'; $this->assertPathEquals($expected, $result); } /** * Test getting layout filenames */ public function testGetLayoutFileName(): void { $viewOptions = [ 'plugin' => null, 'name' => 'Pages', 'viewPath' => 'Pages', 'action' => 'display', ]; $View = new TestView(null, null, null, $viewOptions); $expected = TEST_APP . 'templates' . DS . 'layout' . DS . 'default.php'; $result = $View->getLayoutFileName(); $this->assertPathEquals($expected, $result); $View->setLayoutPath('rss'); $expected = TEST_APP . 'templates' . DS . 'layout' . DS . 'rss' . DS . 'default.php'; $result = $View->getLayoutFileName(); $this->assertPathEquals($expected, $result); $View->setLayoutPath('email' . DS . 'html'); $expected = TEST_APP . 'templates' . DS . 'layout' . DS . 'email' . DS . 'html' . DS . 'default.php'; $result = $View->getLayoutFileName(); $this->assertPathEquals($expected, $result); } /** * Test getting layout filenames for plugins. */ public function testGetLayoutFileNamePlugin(): void { $viewOptions = [ 'plugin' => null, 'name' => 'Pages', 'viewPath' => 'Pages', 'action' => 'display', ]; $View = new TestView(null, null, null, $viewOptions); $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'templates' . DS . 'layout' . DS . 'default.php'; $result = $View->getLayoutFileName('TestPlugin.default'); $this->assertPathEquals($expected, $result); $View->setRequest($View->getRequest()->withParam('plugin', 'TestPlugin')); $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'templates' . DS . 'layout' . DS . 'default.php'; $result = $View->getLayoutFileName('default'); $this->assertPathEquals($expected, $result); } /** * Test getting layout filenames for prefix */ public function testGetLayoutFileNamePrefix(): void { $View = new TestView(); // Prefix specific layout $View->setRequest($View->getRequest()->withParam('prefix', 'foo_prefix')); $expected = TEST_APP . 'templates' . DS . 'FooPrefix' . DS . 'layout' . DS . 'default.php'; $result = $View->getLayoutFileName(); $this->assertPathEquals($expected, $result); $View->setRequest($View->getRequest()->withParam('prefix', 'FooPrefix')); $result = $View->getLayoutFileName(); $this->assertPathEquals($expected, $result); // Nested prefix layout $View->setRequest($View->getRequest()->withParam('prefix', 'foo_prefix/bar_prefix')); $expected = TEST_APP . 'templates' . DS . 'FooPrefix' . DS . 'BarPrefix' . DS . 'layout' . DS . 'default.php'; $result = $View->getLayoutFileName(); $this->assertPathEquals($expected, $result); $expected = TEST_APP . 'templates' . DS . 'FooPrefix' . DS . 'layout' . DS . 'nested_prefix_cascade.php'; $result = $View->getLayoutFileName('nested_prefix_cascade'); $this->assertPathEquals($expected, $result); // Fallback to app's layout $View->setRequest($View->getRequest()->withParam('prefix', 'Admin')); $expected = TEST_APP . 'templates' . DS . 'layout' . DS . 'default.php'; $result = $View->getLayoutFileName(); $this->assertPathEquals($expected, $result); } /** * Test that getLayoutFileName() protects against malicious directory traversal. */ public function testGetLayoutFileNameDirectoryTraversal(): void { $this->expectException(InvalidArgumentException::class); $viewOptions = [ 'plugin' => null, 'name' => 'Pages', 'viewPath' => 'Pages', ]; $view = new TestView(null, null, null, $viewOptions); $view->ext('.php'); $view->getLayoutFileName('../../../bootstrap'); } /** * Test for missing views */ public function testMissingTemplate(): void { $this->expectException(MissingTemplateException::class); $this->expectExceptionMessage('Template file `does_not_exist.php` could not be found'); $this->expectExceptionMessage('The following paths were searched'); $this->expectExceptionMessage('- `' . ROOT . DS . 'templates' . DS . 'does_not_exist.php`'); $viewOptions = [ 'plugin' => null, 'name' => 'Pages', 'viewPath' => 'Pages', ]; $request = new ServerRequest(); $response = new Response(); $View = new TestView($request, $response, null, $viewOptions); $View->getTemplateFileName('does_not_exist'); } /** * Test for missing layouts */ public function testMissingLayout(): void { $this->expectException(MissingLayoutException::class); $this->expectExceptionMessage('Layout file `whatever.php` could not be found'); $this->expectExceptionMessage('The following paths were searched'); $this->expectExceptionMessage('- `' . ROOT . DS . 'templates' . DS . 'layout' . DS . 'whatever.php`'); $viewOptions = [ 'plugin' => null, 'name' => 'Pages', 'viewPath' => 'Pages', 'layout' => 'whatever', ]; $View = new TestView(null, null, null, $viewOptions); $View->getLayoutFileName(); } /** * Test viewVars method */ public function testViewVars(): void { $this->assertEquals(['testData', 'test2', 'test3'], $this->View->getVars()); } /** * Test elementExists method */ public function testElementExists(): void { $result = $this->View->elementExists('test_element'); $this->assertTrue($result); $result = $this->View->elementExists('TestPlugin.plugin_element'); $this->assertTrue($result); $result = $this->View->elementExists('nonexistent_element'); $this->assertFalse($result); $result = $this->View->elementExists('TestPlugin.element'); $this->assertFalse($result); $this->View->setRequest($this->View->getRequest()->withParam('plugin', 'TestPlugin')); $result = $this->View->elementExists('plugin_element'); $this->assertTrue($result); } /** * Test element method */ public function testElement(): void { $result = $this->View->element('test_element'); $this->assertSame('this is the test element', $result); $result = $this->View->element('TestPlugin.plugin_element'); $this->assertSame("Element in the TestPlugin\n", $result); $this->View->setRequest($this->View->getRequest()->withParam('plugin', 'TestPlugin')); $result = $this->View->element('plugin_element'); $this->assertSame("Element in the TestPlugin\n", $result); $result = $this->View->element('plugin_element', [], ['plugin' => false]); $this->assertSame("Plugin element overridden in app\n", $result); } /** * Test element method with a prefix */ public function testPrefixElement(): void { $this->View->setRequest($this->View->getRequest()->withParam('prefix', 'Admin')); $result = $this->View->element('prefix_element'); $this->assertSame('this is a prefixed test element', $result); $result = $this->View->element('TestPlugin.plugin_element'); $this->assertSame('this is the plugin prefixed element using params[plugin]', $result); $this->View->setRequest($this->View->getRequest()->withParam('plugin', 'TestPlugin')); $result = $this->View->element('test_plugin_element'); $this->assertSame('this is the test set using View::$plugin plugin prefixed element', $result); $this->View->setRequest($this->View->getRequest()->withParam('prefix', 'FooPrefix/BarPrefix')); $result = $this->View->element('prefix_element'); $this->assertSame('this is a nested prefixed test element', $result); $result = $this->View->element('prefix_element_in_parent'); $this->assertSame('this is a nested prefixed test element in first level element', $result); } /** * Test loading missing view element */ public function testElementMissing(): void { $this->expectException(MissingElementException::class); $this->expectExceptionMessage('Element file `nonexistent_element.php` could not be found'); $this->View->element('nonexistent_element'); } /** * Test loading nonexistent plugin view element */ public function testElementMissingPluginElement(): void { $this->expectException(MissingElementException::class); $this->expectExceptionMessage('Element file `TestPlugin.nope.php` could not be found'); $this->expectExceptionMessage(implode(DS, ['test_app', 'templates', 'plugin', 'TestPlugin', 'element', 'nope.php'])); $this->expectExceptionMessage(implode(DS, ['test_app', 'Plugin', 'TestPlugin', 'templates', 'element', 'nope.php'])); $this->View->element('TestPlugin.nope'); } /** * Test that elements can have callbacks */ public function testElementCallbacks(): void { $count = 0; $callback = function (EventInterface $event, $file) use (&$count): void { $count++; }; $events = $this->View->getEventManager(); $events->on('View.beforeRender', $callback); $events->on('View.afterRender', $callback); $this->View->element('test_element', [], ['callbacks' => true]); $this->assertSame(2, $count); } /** * Test that additional element viewVars don't get overwritten with helpers. */ public function testElementParamsDontOverwriteHelpers(): void { $Controller = new ViewPostsController(new ServerRequest()); $View = $Controller->createView(); $result = $View->element('type_check', ['form' => 'string'], ['callbacks' => true]); $this->assertSame('string', $result); $View->set('form', 'string'); $result = $View->element('type_check', [], ['callbacks' => true]); $this->assertSame('string', $result); } /** * Test elementCacheHelperNoCache method */ public function testElementCacheHelperNoCache(): void { $Controller = new ViewPostsController(new ServerRequest()); $View = $Controller->createView(); $result = $View->element('test_element', ['ram' => 'val', 'test' => ['foo', 'bar']]); $this->assertSame('this is the test element', $result); } /** * Test elementCache method */ public function testElementCache(): void { Cache::drop('test_view'); Cache::setConfig('test_view', [ 'engine' => 'File', 'duration' => '+1 day', 'path' => CACHE . 'views/', 'prefix' => '', ]); Cache::clear('test_view'); $View = $this->PostsController->createView(); $View->setElementCache('test_view'); $result = $View->element('test_element', [], ['cache' => true]); $expected = 'this is the test element'; $this->assertSame($expected, $result); $result = Cache::read('element__test_element', 'test_view'); $this->assertSame($expected, $result); $result = $View->element('test_element', ['param' => 'one', 'foo' => 'two'], ['cache' => true]); $this->assertSame($expected, $result); $result = Cache::read('element__test_element_param_foo', 'test_view'); $this->assertSame($expected, $result); $View->element('test_element', [ 'param' => 'one', 'foo' => 'two', ], [ 'cache' => ['key' => 'custom_key'], ]); $result = Cache::read('element_custom_key', 'test_view'); $this->assertSame($expected, $result); $View->setElementCache('default'); $View->element('test_element', [ 'param' => 'one', 'foo' => 'two', ], [ 'cache' => ['config' => 'test_view'], ]); $result = Cache::read('element__test_element_param_foo', 'test_view'); $this->assertSame($expected, $result); Cache::clear('test_view'); Cache::drop('test_view'); } /** * Test elementCache method with namespaces and subfolder */ public function testElementCacheSubfolder(): void { Cache::drop('test_view'); Cache::setConfig('test_view', [ 'engine' => 'File', 'duration' => '+1 day', 'path' => CACHE . 'views/', 'prefix' => '', ]); Cache::clear('test_view'); $View = $this->PostsController->createView(); $View->setElementCache('test_view'); $result = $View->element('subfolder/test_element', [], ['cache' => true]); $expected = 'this is the test element in subfolder'; $this->assertSame($expected, trim($result)); $result = Cache::read('element__subfolder_test_element', 'test_view'); $this->assertSame($expected, trim($result)); } /** * Test element events */ public function testViewEvent(): void { $View = $this->PostsController->createView(); $View->setTemplatePath($this->PostsController->getName()); $View->enableAutoLayout(false); $listener = new TestViewEventListenerInterface(); $View->getEventManager()->on($listener); $View->render('index'); $this->assertSame(View::TYPE_TEMPLATE, $listener->beforeRenderViewType); $this->assertSame(View::TYPE_TEMPLATE, $listener->afterRenderViewType); $this->assertSame($View->getCurrentType(), View::TYPE_TEMPLATE); $View->element('test_element', [], ['callbacks' => true]); $this->assertSame($View->getCurrentType(), View::TYPE_TEMPLATE); $this->assertSame(View::TYPE_ELEMENT, $listener->beforeRenderViewType); $this->assertSame(View::TYPE_ELEMENT, $listener->afterRenderViewType); } /** * Test adding helpers in initialize(). */ public function testAddHelper(): void { $View = new TestView(); $this->assertInstanceOf(HtmlHelper::class, $View->Html); $config = $View->Html->getConfig(); $this->assertSame('myval', $config['mykey']); } /** * Test loading helper using loadHelper(). */ public function testLoadHelper(): void { $View = new View(); $View->loadHelper('Html', ['foo' => 'bar']); $this->assertInstanceOf(HtmlHelper::class, $View->Html); $config = $View->Html->getConfig(); $this->assertSame('bar', $config['foo']); } /** * Test loading helper when duplicate. */ public function testLoadHelperDuplicate(): void { $View = new View(); $this->assertNotEmpty($View->loadHelper('Html', ['foo' => 'bar'])); try { $View->loadHelper('Html', ['test' => 'value']); $this->fail('No exception'); } catch (RuntimeException $e) { $this->assertStringContainsString('The `Html` alias has already been loaded', $e->getMessage()); } } /** * Test loadHelpers method */ public function testLoadHelpers(): void { $View = new View(null, null, null, [ 'helpers' => ['Html' => ['foo' => 'bar'], 'Form' => ['foo' => 'baz']], ]); $result = $View->loadHelpers(); $this->assertSame($View, $result); $this->assertInstanceOf(HtmlHelper::class, $View->Html, 'Object type is wrong.'); $this->assertInstanceOf(FormHelper::class, $View->Form, 'Object type is wrong.'); $config = $View->Html->getConfig(); $this->assertSame('bar', $config['foo']); $config = $View->Form->getConfig(); $this->assertSame('baz', $config['foo']); } /** * Test lazy loading helpers */ public function testLazyLoadHelpers(): void { $View = new View(); $this->assertInstanceOf(HtmlHelper::class, $View->Html, 'Object type is wrong.'); $this->assertInstanceOf(FormHelper::class, $View->Form, 'Object type is wrong.'); } /** * Test lazy loading helpers in plugins */ public function testLazyLoadHelpersPlugin(): void { $view = new View(null, null, null, ['plugin' => 'TestPlugin']); $this->assertEquals('hello other', $view->OtherHelper->hello()); $this->assertEquals('hello plugged, hello other', $view->PluggedHelper->hello()); } /** * Test manipulating class properties in initialize() */ public function testInitialize(): void { $View = new TestView(); $config = $View->Html->getConfig(); $this->assertSame('myval', $config['mykey']); } /** * Test the correct triggering of helper callbacks */ public function testHelperCallbackTriggering(): void { $View = $this->PostsController->createView(); $View->setTemplatePath($this->PostsController->getName()); $manager = $this->getMockBuilder(EventManager::class)->getMock(); $View->setEventManager($manager); $manager->expects($this->exactly(8)) ->method('dispatch') ->with( ...self::withConsecutive( [$this->callback(function (EventInterface $event) { return $event->getName() === 'View.beforeRender'; })], [$this->callback(function (EventInterface $event) { return $event->getName() === 'View.beforeRenderFile'; })], [$this->callback(function (EventInterface $event) { return $event->getName() === 'View.afterRenderFile'; })], [$this->callback(function (EventInterface $event) { return $event->getName() === 'View.afterRender'; })], [$this->callback(function (EventInterface $event) { return $event->getName() === 'View.beforeLayout'; })], [$this->callback(function (EventInterface $event) { return $event->getName() === 'View.beforeRenderFile'; })], [$this->callback(function (EventInterface $event) { return $event->getName() === 'View.afterRenderFile'; })], [$this->callback(function (EventInterface $event) { return $event->getName() === 'View.afterLayout'; })] ) ); $View->render('index'); } /** * Test beforeLayout method */ public function testBeforeLayout(): void { $this->PostsController->viewBuilder()->setHelpers([ 'TestBeforeAfter' => ['className' => TestBeforeAfterHelper::class], 'Html', ]); $View = $this->PostsController->createView(); $View->setTemplatePath($this->PostsController->getName()); $View->render('index'); $this->assertSame('Valuation', $View->helpers()->TestBeforeAfter->property); } /** * Test afterLayout method */ public function testAfterLayout(): void { $this->PostsController->viewBuilder()->setHelpers([ 'TestBeforeAfter' => ['className' => TestBeforeAfterHelper::class], 'Html', ]); $this->PostsController->set('variable', 'values'); $View = $this->PostsController->createView(); $View->setTemplatePath($this->PostsController->getName()); $content = 'This is my view output'; $result = $View->renderLayout($content, 'default'); $this->assertMatchesRegularExpression('/modified in the afterlife/', $result); $this->assertMatchesRegularExpression('/This is my view output/', $result); } /** * Test renderLoadHelper method */ public function testRenderLoadHelper(): void { $this->PostsController->viewBuilder()->addHelpers(['Form', 'Number']); $View = $this->PostsController->createView(TestView::class); $View->setTemplatePath($this->PostsController->getName()); $result = $View->render('index', false); $this->assertSame('posts index', $result); $attached = $View->helpers()->loaded(); // HtmlHelper is loaded in TestView::initialize() $this->assertEquals(['Form', 'Number', 'Html'], $attached); $this->PostsController->viewBuilder()->addHelpers( ['Html', 'Form', 'Number', 'TestPlugin.PluggedHelper'] ); $View = $this->PostsController->createView(TestView::class); $View->setTemplatePath($this->PostsController->getName()); $result = $View->render('index', false); $this->assertSame('posts index', $result); $attached = $View->helpers()->loaded(); $expected = ['Form', 'Number', 'Html', 'PluggedHelper']; $this->assertEquals($expected, $attached, 'Attached helpers are wrong.'); } /** * Test render method */ public function testRender(): void { $View = $this->PostsController->createView(TestView::class); $View->setTemplatePath($this->PostsController->getName()); $result = $View->render('index'); $this->assertMatchesRegularExpression("/\s*