diff --git a/composer.json b/composer.json index d4119e2f..117dc1e3 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,8 @@ }, "require-dev": { "ezsystems/ezpublish-kernel": "^6.0", - "phpunit/phpunit": "^4.8|^5.7|^6.5" + "phpunit/phpunit": "^4.8|^5.7|^6.5", + "matthiasnoback/symfony-dependency-injection-test": "^1.0" }, "conflict": { "netgen/block-manager": "<0.8", diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 6a4fac1a..258d814d 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -21,12 +21,10 @@ bundle/Command bundle/Controller - bundle/DependencyInjection bundle/EventListener bundle/Exception bundle/Installer bundle/Resources - bundle/Templating bundle/NetgenAdminUIBundle.php diff --git a/tests/DependencyInjection/CompilerPass/MenuPluginRegistryPassTest.php b/tests/DependencyInjection/CompilerPass/MenuPluginRegistryPassTest.php new file mode 100644 index 00000000..419b13e5 --- /dev/null +++ b/tests/DependencyInjection/CompilerPass/MenuPluginRegistryPassTest.php @@ -0,0 +1,38 @@ +assertNull($this->compile()); + } + + public function testCompile() + { + $definition = new Definition(); + $definition->addTag('netgen_admin_ui.menu_plugin', array('priority' => 2)); + + $this->setDefinition('netgen_admin_ui.menu_plugin.registry', $definition); + + $this->compile(); + + $arguments = array( + new Reference('netgen_admin_ui.menu_plugin.registry'), + ); + + $this->assertContainerBuilderHasServiceDefinitionWithMethodCall('netgen_admin_ui.menu_plugin.registry', 'addMenuPlugin', $arguments); + } + + protected function registerCompilerPass(ContainerBuilder $container) + { + $container->addCompilerPass(new MenuPluginRegistryPass()); + } +} diff --git a/tests/DependencyInjection/NetgenAdminUIExtensionTest.php b/tests/DependencyInjection/NetgenAdminUIExtensionTest.php new file mode 100644 index 00000000..1bad5873 --- /dev/null +++ b/tests/DependencyInjection/NetgenAdminUIExtensionTest.php @@ -0,0 +1,55 @@ +setExpectedException('RuntimeException'); + + $this->container->setParameter('kernel.bundles', []); + $this->load(); + } + + public function testWithNoInformationCollectionBundle() + { + $this->container->setParameter('kernel.bundles', array( + 'EzCoreExtraBundle' => 'EzCoreExtraBundle', + 'NetgenBlockManagerBundle' => 'NetgenBlockManagerBundle', + )); + + $this->load(); + + $this->assertContainerBuilderHasParameter('netgen_admin_ui.logo_type', 'default'); + } + + public function testWithAdminUILogo() + { + $this->container->setParameter('kernel.bundles', array( + 'EzCoreExtraBundle' => 'EzCoreExtraBundle', + 'NetgenBlockManagerBundle' => 'NetgenBlockManagerBundle', + 'NetgenInformationCollectionBundle' => 'NetgenInformationCollectionBundle', + )); + + $this->container->setParameter('netgen_admin_ui.logo_type', 'default'); + + $this->getMockBuilder('Netgen\Bundle\MoreBundle\NetgenMoreBundle') + ->disableOriginalConstructor() + ->getMock(); + + $this->load(); + + $this->assertContainerBuilderHasParameter('netgen_admin_ui.logo_type', 'ngadminui'); + } + + protected function getContainerExtensions() + { + return [ + new NetgenAdminUIExtension(), + ]; + } +} diff --git a/tests/Layouts/EventListener/CacheEnabledListenerTest.php b/tests/Layouts/EventListener/CacheEnabledListenerTest.php new file mode 100644 index 00000000..776d627a --- /dev/null +++ b/tests/Layouts/EventListener/CacheEnabledListenerTest.php @@ -0,0 +1,93 @@ +markTestSkipped(); + } + + $httpCacheClient = $this->getMockBuilder('Netgen\BlockManager\HttpCache\ClientInterface') + ->disableOriginalConstructor() + ->getMock(); + + $this->listener = new CacheEnabledListener($httpCacheClient); + } + + public function testGetSubscribedEvents() + { + $this->assertEquals(array(BlockManagerEvents::BUILD_VIEW => 'onBuildView'), CacheEnabledListener::getSubscribedEvents()); + } + + public function testOnBuildViewWithWrongView() + { + $view = new FormView(); + $event = new CollectViewParametersEvent($view); + + $this->assertNull($this->listener->onBuildView($event)); + } + + public function testOnBuildViewWithWrongViewContext() + { + $view = new LayoutView(); + $view->setContext('unsupported'); + $event = new CollectViewParametersEvent($view); + + $this->assertNull($this->listener->onBuildView($event)); + } + + public function testOnBuildViewEnabled() + { + $view = new LayoutView(); + $view->setContext('ngadminui'); + $event = new CollectViewParametersEvent($view); + + $this->assertNull($this->listener->onBuildView($event)); + + $parameters = array( + 'http_cache_enabled' => true, + ); + + $this->assertEquals($parameters, $event->getParameters()); + } + + public function testOnBuildViewDisabled() + { + $listener = new CacheEnabledListener(new NullClient()); + + $view = new LayoutView(); + $view->setContext('ngadminui'); + $event = new CollectViewParametersEvent($view); + + $this->assertNull($listener->onBuildView($event)); + + $parameters = array( + 'http_cache_enabled' => false, + ); + + $this->assertEquals($parameters, $event->getParameters()); + } +} diff --git a/tests/Layouts/RelatedLayoutsLoaderTest.php b/tests/Layouts/RelatedLayoutsLoaderTest.php new file mode 100644 index 00000000..a9de05d0 --- /dev/null +++ b/tests/Layouts/RelatedLayoutsLoaderTest.php @@ -0,0 +1,159 @@ +markTestSkipped(); + } + + $this->layoutService = $this->getMockBuilder('Netgen\BlockManager\API\Service\LayoutService') + ->disableOriginalConstructor() + ->getMock(); + + $this->databaseConnection = $this->getMockBuilder('Doctrine\DBAL\Connection') + ->disableOriginalConstructor() + ->getMock(); + + $this->loader = new RelatedLayoutsLoader($this->layoutService, $this->databaseConnection); + } + + public function testLoadRelatedLayouts() + { + $contentInfo = new ContentInfo(); + + $location = new Location( + array( + 'contentInfo' => $contentInfo, + ) + ); + + $queryBuilder = $this->getMockBuilder('Doctrine\DBAL\Query\QueryBuilder') + ->disableOriginalConstructor() + ->getMock(); + + $expressionBuilder = $this->getMockBuilder('Doctrine\DBAL\Query\Expression\ExpressionBuilder') + ->disableOriginalConstructor() + ->getMock(); + + $statement = $this->getMockBuilder('Doctrine\DBAL\Driver\Statement') + ->disableOriginalConstructor() + ->getMock(); + + $this->databaseConnection + ->expects($this->once()) + ->method('createQueryBuilder') + ->willReturn($queryBuilder); + + $queryBuilder + ->expects($this->once()) + ->method('select') + ->willReturn($queryBuilder); + + $queryBuilder + ->expects($this->once()) + ->method('from') + ->willReturn($queryBuilder); + + $queryBuilder + ->expects($this->exactly(2)) + ->method('innerJoin') + ->willReturn($queryBuilder); + + $queryBuilder + ->expects($this->exactly(15)) + ->method('expr') + ->willReturn($expressionBuilder); + + $queryBuilder + ->expects($this->once()) + ->method('where') + ->willReturn($queryBuilder); + + $queryBuilder + ->expects($this->exactly(5)) + ->method('setParameter') + ->willReturn($queryBuilder); + + $expressionBuilder + ->expects($this->exactly(5)) + ->method('andX'); + + $expressionBuilder + ->expects($this->once()) + ->method('orX'); + + $expressionBuilder + ->expects($this->exactly(9)) + ->method('eq'); + + $queryBuilder + ->expects($this->once()) + ->method('execute') + ->willReturn($statement); + + $layoutRows = array( + array( + 'layout_id' => 5, + ), + array( + 'layout_id' => 6, + ), + array( + 'layout_id' => 7, + ) + ); + + $layout1 = new Layout(array('name' => 'Frontpage')); + $layout2 = new Layout(array('name' => 'Article')); + $layout3 = new Layout(array('name' => 'Article')); + + $statement + ->expects($this->once()) + ->method('fetchAll') + ->willReturn($layoutRows); + + $this->layoutService + ->expects($this->exactly(3)) + ->method('loadLayout') + ->withConsecutive( + array(5), + array(6), + array(7) + ) + ->willReturnOnConsecutiveCalls($layout1, $layout2, $layout3); + + $result = array($layout2, $layout3, $layout1); + + $this->assertEquals($result, $this->loader->loadRelatedLayouts($location)); + } +} diff --git a/tests/MenuPlugin/InformationCollectionMenuPluginTest.php b/tests/MenuPlugin/InformationCollectionMenuPluginTest.php new file mode 100644 index 00000000..c0ab50a0 --- /dev/null +++ b/tests/MenuPlugin/InformationCollectionMenuPluginTest.php @@ -0,0 +1,79 @@ + 'NetgenInformationCollectionBundle', + 'NetgenRemoteMediaBundle' => 'NetgenRemoteMediaBundle', + 'NetgenTagsBundle' => 'NetgenTagsBundle', + ); + + $this->plugin = new InformationCollectionMenuPlugin($activatedBundles, true); + } + + public function testGetIdentifier() + { + $this->assertEquals('information_collection', $this->plugin->getIdentifier()); + } + + public function testGetTemplates() + { + $result = array( + 'aside' => '@NetgenAdminUI/menu/plugins/information_collection/aside.html.twig', + 'left' => '@NetgenAdminUI/menu/plugins/information_collection/left.html.twig', + ); + + $this->assertEquals($result, $this->plugin->getTemplates()); + } + + public function testIsActive() + { + $this->assertTrue($this->plugin->isActive()); + } + + public function testIsActiveNoBundle() + { + $activatedBundles = array( + 'NetgenRemoteMediaBundle' => 'NetgenRemoteMediaBundle', + 'NetgenTagsBundle' => 'NetgenTagsBundle', + ); + + $plugin = new InformationCollectionMenuPlugin($activatedBundles, true); + + $this->assertFalse($plugin->isActive()); + } + + public function testMatches() + { + $request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request') + ->disableOriginalConstructor() + ->getMock(); + + $parameterBag = $this->getMockBuilder('Symfony\Component\HttpFoundation\ParameterBag') + ->disableOriginalConstructor() + ->getMock(); + + $request->attributes = $parameterBag; + + $parameterBag + ->expects($this->once()) + ->method('get') + ->with('_route') + ->willReturn('netgen_information_collection'); + + $this->assertTrue($this->plugin->matches($request)); + } +} diff --git a/tests/Templating/GlobalVariableTest.php b/tests/Templating/GlobalVariableTest.php new file mode 100644 index 00000000..da61d732 --- /dev/null +++ b/tests/Templating/GlobalVariableTest.php @@ -0,0 +1,149 @@ +menuPluginRegistry = $this->getMockBuilder('Netgen\Bundle\AdminUIBundle\MenuPlugin\MenuPluginRegistry') + ->disableOriginalConstructor() + ->getMock(); + + $this->requestStack = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack') + ->disableOriginalConstructor() + ->getMock(); + + $this->logoType = 'logo1'; + + $this->globalVariable = new GlobalVariable($this->menuPluginRegistry, $this->requestStack, $this->logoType); + } + + public function testGetMenuPlugins() + { + $menuPlugins = array( + 'menu_plugin_1', + 'menu_plugin_2', + 'menu_plugin_3', + ); + + $this->menuPluginRegistry + ->expects($this->once()) + ->method('getMenuPlugins') + ->willReturn($menuPlugins) + ; + + $this->assertEquals($menuPlugins, $this->globalVariable->getMenuPlugins()); + } + + public function testGetLogoType() + { + $this->assertEquals($this->logoType, $this->globalVariable->getLogoType()); + } + + public function testGetCurrentMenuPluginNoRequest() + { + $this->requestStack + ->expects($this->once()) + ->method('getCurrentRequest') + ->willReturn(null) + ; + + $this->assertFalse($this->globalVariable->getCurrentMenuPlugin()); + } + + public function testGetCurrentMenuPlugin() + { + $request = new Request(); + + $menuPlugin = $this->getMockBuilder('Netgen\Bundle\AdminUIBundle\MenuPlugin\MenuPluginInterface') + ->disableOriginalConstructor() + ->getMock(); + + $menuPlugins = array( + 'menu_plugin_1' => $menuPlugin, + 'menu_plugin_2' => $menuPlugin, + 'menu_plugin_3' => $menuPlugin, + ); + + $this->requestStack + ->expects($this->once()) + ->method('getCurrentRequest') + ->willReturn($request) + ; + + $this->menuPluginRegistry + ->expects($this->once()) + ->method('getMenuPlugins') + ->willReturn($menuPlugins) + ; + + $menuPlugin + ->expects($this->exactly(2)) + ->method('matches') + ->willReturnOnConsecutiveCalls(false, true) + ; + + $this->assertEquals('menu_plugin_2', $this->globalVariable->getCurrentMenuPlugin()); + } + + public function testGetCurrentMenuPluginNoMatch() + { + $request = new Request(); + + $menuPlugin = $this->getMockBuilder('Netgen\Bundle\AdminUIBundle\MenuPlugin\MenuPluginInterface') + ->disableOriginalConstructor() + ->getMock(); + + $menuPlugins = array( + 'menu_plugin_1' => $menuPlugin, + 'menu_plugin_2' => $menuPlugin, + 'menu_plugin_3' => $menuPlugin, + ); + + $this->requestStack + ->expects($this->once()) + ->method('getCurrentRequest') + ->willReturn($request) + ; + + $this->menuPluginRegistry + ->expects($this->once()) + ->method('getMenuPlugins') + ->willReturn($menuPlugins) + ; + + $menuPlugin + ->expects($this->exactly(3)) + ->method('matches') + ->with($request) + ->willReturnOnConsecutiveCalls(false, false, false) + ; + + $this->assertFalse($this->globalVariable->getCurrentMenuPlugin()); + } +} diff --git a/tests/Templating/Twig/Extension/NetgenAdminUIExtensionTest.php b/tests/Templating/Twig/Extension/NetgenAdminUIExtensionTest.php new file mode 100644 index 00000000..a967e463 --- /dev/null +++ b/tests/Templating/Twig/Extension/NetgenAdminUIExtensionTest.php @@ -0,0 +1,93 @@ +pathHelper = $this->getMockBuilder('Netgen\Bundle\AdminUIBundle\Helper\PathHelper') + ->disableOriginalConstructor() + ->getMock(); + + $this->mockedKernel = $this->getMockBuilder('stdClass') + ->setMethods(array('runCallback')) + ->getMock(); + + $callbackMock = function () { + return $this->mockedKernel; + }; + + $this->extension = new NetgenAdminUIExtension($this->pathHelper, $callbackMock); + } + + public function testGetName() + { + $this->assertEquals('netgen_admin_ui', $this->extension->getName()); + } + + public function testGetFunctions() + { + $this->assertNotEmpty($this->extension->getFunctions()); + + foreach ($this->extension->getFunctions() as $function) { + $this->assertInstanceOf('Twig_SimpleFunction', $function); + } + } + + public function testGetLocationPath() + { + $locationId = 20; + $path = '/2/15/20'; + + $this->pathHelper + ->expects($this->once()) + ->method('getPath') + ->with($locationId) + ->willReturn($path) + ; + + $this->assertEquals($path, $this->extension->getLocationPath($locationId)); + } + + public function testGetLegacyPreference() + { + $name = 'test'; + + $callback = function () use ($name) { + return $name; + }; + + $this->mockedKernel + ->expects($this->once()) + ->method('runCallback') + ->willReturnCallback($callback); + + $this->assertEquals($name, $this->extension->getLegacyPreference($name)); + } +}