/** * @dataProvider pathsDataProvider * * @param string[] $existingPaths * @param string|null $widget * @param string[] $expectedPaths */ public function testGetPaths($existingPaths, $widget, $expectedPaths) { $context = new LayoutContext(); $context->set('widget_container', $widget); $this->provider->setContext($context); $this->assertSame($expectedPaths, $this->provider->getPaths($existingPaths)); }
/** * Renders the layout and initializes the content of a new response object * with the rendered layout. * * @param GetResponseForControllerResultEvent $event * * @throws LogicException if @Layout annotation is used in incorrect way */ public function onKernelView(GetResponseForControllerResultEvent $event) { $request = $event->getRequest(); /** @var LayoutAnnotation|null $layoutAnnotation */ $layoutAnnotation = $request->attributes->get('_layout'); if (!$layoutAnnotation) { return; } if ($request->attributes->get('_template')) { throw new LogicException('The @Template() annotation cannot be used together with the @Layout() annotation.'); } $parameters = $event->getControllerResult(); if (is_array($parameters)) { $context = new LayoutContext(); foreach ($parameters as $key => $value) { $context->set($key, $value); } $this->configureContext($context, $layoutAnnotation); $layout = $this->getLayout($context, $layoutAnnotation); } elseif ($parameters instanceof ContextInterface) { $this->configureContext($parameters, $layoutAnnotation); $layout = $this->getLayout($parameters, $layoutAnnotation); } elseif ($parameters instanceof Layout) { if (!$layoutAnnotation->isEmpty()) { throw new LogicException('The empty @Layout() annotation must be used when ' . 'the controller returns an instance of "Oro\\Component\\Layout\\Layout".'); } $layout = $parameters; } else { return; } $response = new Response(); $response->setContent($layout->render()); $event->setResponse($response); }
/** * @param bool $evaluateDeferred * @return LayoutContext */ protected function getContextMock($evaluateDeferred) { $context = new LayoutContext(); $context->set('expressions_evaluate_deferred', $evaluateDeferred); $context->set('expressions_evaluate', true); $context->set('expressions_encoding', 'json'); return $context; }
/** * @dataProvider pathsDataProvider * * @param array $expectedResults * @param string|null $theme * @param string|null $route * @param string|null $action */ public function testGetPaths(array $expectedResults, $theme, $route, $action) { $context = new LayoutContext(); $context->set('theme', $theme); $context->set('route_name', $route); $context->set('action', $action); $this->setUpThemeManager(['black' => $this->getThemeMock('black', 'base'), 'base' => $this->getThemeMock('base')]); $this->provider->setContext($context); $this->assertSame($expectedResults, $this->provider->getPaths([])); }
public function testConfigureContextWithRequestAndDataSetInContext() { $context = new LayoutContext(); $context->set('theme', 'themeShouldNotBeOverridden'); $request = Request::create(''); $request->attributes->set('_theme', 'testTheme'); $this->contextConfigurator->setRequest($request); $this->contextConfigurator->configureContext($context); $context->resolve(); $this->assertSame('themeShouldNotBeOverridden', $context->get('theme')); }
public function testFinishViewRemovesEmptyClassWhenEncodingEnabled() { $context = new LayoutContext(); $context->set('css_class', 'test_class'); $block = $this->getMock('Oro\\Component\\Layout\\BlockInterface'); $block->expects($this->once())->method('getContext')->will($this->returnValue($context)); $view = new BlockView(); $view->vars['attr']['class'] = ''; $context['expressions_evaluate'] = false; $context['expressions_encoding'] = 'json'; $this->extension->finishView($view, $block, []); $this->assertArrayNotHasKey('class', $view->vars['attr']); }
public function testConfigureContextOverride() { $request = new Request(); $request->query->set('_wid', 'test_widget_id'); $request->query->set('_widgetContainer', 'dialog'); $this->requestStack->push($request); $context = new LayoutContext(); $context['widget_container'] = 'updated_widget'; $context->data()->set('widget_id', 'updated_id', 'updated_widget_id'); $this->contextConfigurator->configureContext($context); $context->resolve(); $this->assertEquals('updated_widget', $context['widget_container']); $this->assertEquals('updated_id', $context->data()->getIdentifier('widget_id')); $this->assertEquals('updated_widget_id', $context->data()->get('widget_id')); }
public function testHtmlRenderingForFormStartByPhpRenderer() { if (!$this->getContainer()->hasParameter('oro_layout.twig.resources')) { $this->markTestSkipped('TWIG renderer is not enabled.'); } $context = new LayoutContext(); $context->getResolver()->setOptional(['form']); $form = $this->getTestForm(); $context->set('form', new FormAccessor($form, FormAction::createByPath('test.php'), 'patch')); // revert TWIG form renderer to Symfony's default theme $this->getContainer()->get('twig.form.renderer')->setTheme($context->get('form')->getView(), 'form_div_layout.html.twig'); $layoutManager = $this->getContainer()->get('oro_layout.layout_manager'); $result = $layoutManager->getLayoutBuilder()->add('form:start', null, 'form_start')->getLayout($context)->setRenderer('php')->render(); $expected = $this->getFormStartTestLayoutResult(); $this->assertHtmlEquals($expected, $result); }
public function testArrayAccessExistsForContextData() { $name = 'foo'; $this->context->data()->set($name, 'foo_id', 'val'); $this->context->resolve(); $this->registry->expects($this->once())->method('findDataProvider')->with($name)->will($this->returnValue(null)); $this->assertTrue(isset($this->dataAccessor[$name])); }
/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testFinishViewEvaluatesAllExpressions() { $context = new LayoutContext(); $context->set('css_class', 'test_class'); $data = $this->getMock('Oro\\Component\\Layout\\DataAccessorInterface'); $block = $this->getMock('Oro\\Component\\Layout\\BlockInterface'); $block->expects($this->once())->method('getContext')->will($this->returnValue($context)); $block->expects($this->once())->method('getData')->will($this->returnValue($data)); $view = new BlockView(); $expr = $this->getMock('Oro\\Component\\ConfigExpression\\ExpressionInterface'); $expr->expects($this->once())->method('evaluate')->with(['context' => $context, 'data' => $data])->will($this->returnValue(true)); $classExpr = new Func\GetValue(); $classExpr->initialize([new PropertyPath('context.css_class')]); $classExpr->setContextAccessor(new ContextAccessor()); $classAttr = new OptionValueBag(); $classAttr->add(['@value' => ['$context.css_class']]); $expectedClassAttr = new OptionValueBag(); $expectedClassAttr->add('test_class'); $view->vars['expr_object'] = $expr; $view->vars['expr_array'] = ['@true' => null]; $view->vars['not_expr_array'] = ['\\@true' => null]; $view->vars['scalar'] = 123; $view->vars['attr']['enabled'] = ['@true' => null]; $view->vars['attr']['data-scalar'] = 'foo'; $view->vars['attr']['data-expr'] = ['@true' => null]; $view->vars['attr']['class'] = $classAttr; $view->vars['label_attr']['enabled'] = ['@true' => null]; $view->vars['array_with_expr'] = ['item1' => 'val1', 'item2' => ['@true' => null]]; $this->expressionAssembler->expects($this->exactly(6))->method('assemble')->will($this->returnValueMap([[['@true' => null], new Condition\True()], [['@value' => ['$context.css_class']], $classExpr]])); $context['expressions_evaluate'] = true; $this->extension->finishView($view, $block, []); $this->assertSame(true, $view->vars['expr_object'], 'Failed asserting that an expression is evaluated'); $this->assertSame(true, $view->vars['expr_array'], 'Failed asserting that an expression is assembled and evaluated'); $this->assertSame(['@true' => null], $view->vars['not_expr_array'], 'Failed asserting that a backslash at the begin of the array key is removed'); $this->assertSame(123, $view->vars['scalar'], 'Failed asserting that a scalar value is not changed'); $this->assertSame(true, $view->vars['attr']['enabled'], 'Failed asserting that an expression in "attr" is assembled and evaluated'); $this->assertSame('foo', $view->vars['attr']['data-scalar'], 'Failed asserting that "attr.data-scalar" exists'); $this->assertSame(true, $view->vars['attr']['data-expr'], 'Failed asserting that "attr.data-expr" is assembled and evaluated'); $this->assertEquals($expectedClassAttr, $view->vars['attr']['class'], 'Failed asserting that "attr.class" is assembled and evaluated'); $this->assertSame(true, $view->vars['label_attr']['enabled'], 'Failed asserting that an expression in "label_attr" is assembled and evaluated'); $this->assertSame(['item1' => 'val1', 'item2' => true], $view->vars['array_with_expr'], 'Failed asserting that an expression is assembled and evaluated in nested array'); }
/** * @param EmbeddedForm $formEntity * @param FormInterface $form * * @return Layout */ public function getLayout(EmbeddedForm $formEntity, FormInterface $form = null) { $formTypeName = $formEntity->getFormType(); $customLayout = $this->formManager->getCustomFormLayoutByFormType($formTypeName); $layoutContext = new LayoutContext(); $layoutContext->getResolver()->setRequired(['embedded_form_type'])->setOptional(['embedded_form', 'embedded_form_custom_layout']); $layoutContext->set('theme', 'embedded_default'); $layoutContext->set('embedded_form', null === $form ? null : new FormAccessor($form)); $layoutContext->set('embedded_form_type', $formTypeName); $layoutContext->set('embedded_form_custom_layout', $customLayout); $layoutContext->data()->set('embedded_form_entity', '', $formEntity); $layoutBuilder = $this->layoutManager->getLayoutBuilder(); // TODO discuss adding root automatically $layoutBuilder->add('root', null, 'root'); return $layoutBuilder->getLayout($layoutContext); }
public function testFinishViewWhenFormBlockIsRoot() { $formLayoutBuilder = $this->getMock('Oro\\Bundle\\LayoutBundle\\Layout\\Form\\FormLayoutBuilderInterface'); $type = new FormType($formLayoutBuilder); $formName = 'form'; $view = new BlockView(); $block = $this->getMock('Oro\\Component\\Layout\\BlockInterface'); $formAccessor = $this->getMock('Oro\\Bundle\\LayoutBundle\\Layout\\Form\\FormAccessorInterface'); $context = new LayoutContext(); $formView = new FormView(); $view->vars['form'] = $formView; $formView->children['field1'] = new FormView($formView); $formView->children['field2'] = new FormView($formView); $field3View = new FormView($formView); $formView->children['field3'] = $field3View; $field3View->children['field31'] = new FormView($field3View); $field3View->children['field32'] = new FormView($field3View); $view->children['block1'] = new BlockView($view); $view->children['block1']->vars['form'] = $formView['field1']; $view->children['block3'] = new BlockView($view); $view->children['block3']->vars['form'] = $field3View['field31']; $this->setLayoutBlocks(['root' => $view]); $context->set('form', $formAccessor); $block->expects($this->once())->method('getContext')->will($this->returnValue($context)); $formAccessor->expects($this->once())->method('getProcessedFields')->will($this->returnValue(['field1' => 'block1', 'field2' => 'block2', 'field3.field31' => 'block3', 'field3.field32' => 'block4'])); $type->finishView($view, $block, ['form_name' => $formName]); $this->assertFalse($formView->isRendered()); $this->assertFalse($formView['field1']->isRendered()); $this->assertTrue($formView['field2']->isRendered()); $this->assertFalse($formView['field3']['field31']->isRendered()); $this->assertTrue($formView['field3']['field32']->isRendered()); }
public function testShouldThrowExceptionForNotHavingRequiredVarsWhenContextReturned() { $this->setupLayoutExpectations(); $attributes = ['_layout' => new LayoutAnnotation(['vars' => ['required1', 'required2']])]; $context = new LayoutContext(); $context->getResolver()->setRequired(['required2']); $context['required2'] = 'value1'; $responseEvent = $this->createResponseForControllerResultEvent($attributes, $context); $this->listener->onKernelView($responseEvent); }
public function testGetData() { $this->assertInstanceOf('Oro\\Component\\Layout\\ContextDataCollection', $this->context->data()); }
public function testProcessExpressionsEncodesAllExpressions() { $context = new LayoutContext(); $context->set('expressions_evaluate_deferred', true); $data = $this->getMock('Oro\\Component\\Layout\\DataAccessorInterface'); $expr = $this->getMock('Oro\\Component\\ConfigExpression\\ExpressionInterface'); $expr->expects($this->once())->method('toArray')->will($this->returnValue(['@true' => null])); $classExpr = new Func\GetValue(); $classExpr->initialize([new PropertyPath('context.css_class')]); $classAttr = new OptionValueBag(); $classAttr->add(['@value' => ['$context.css_class']]); $expectedClassAttr = new OptionValueBag(); $expectedClassAttr->add('{"@value":{"parameters":["$context.css_class"]}}'); $values['expr_object'] = $expr; $values['expr_array'] = ['@true' => null]; $values['not_expr_array'] = ['\\@true' => null]; $values['scalar'] = 123; $values['attr']['enabled'] = ['@true' => null]; $values['attr']['class'] = $classAttr; $values['label_attr']['enabled'] = ['@true' => null]; $this->expressionAssembler->expects($this->exactly(4))->method('assemble')->will($this->returnValueMap([[['@true' => null], new Condition\True()], [['@value' => ['$context.css_class']], $classExpr]])); $this->processor->processExpressions($values, $context, $data, false, 'json'); $this->assertSame('{"@true":null}', $values['expr_object'], 'Failed asserting that an expression is encoded'); $this->assertSame('{"@true":null}', $values['expr_array'], 'Failed asserting that an expression is assembled and encoded'); $this->assertSame(['@true' => null], $values['not_expr_array'], 'Failed asserting that a backslash at the begin of the array key is removed'); $this->assertSame(123, $values['scalar'], 'Failed asserting that a scalar value is not changed'); $this->assertSame('{"@true":null}', $values['attr']['enabled'], 'Failed asserting that an expression in "attr" is assembled and encoded'); $this->assertEquals($expectedClassAttr, $values['attr']['class'], 'Failed asserting that "attr.class" is assembled and encoded'); $this->assertSame('{"@true":null}', $values['label_attr']['enabled'], 'Failed asserting that an expression in "label_attr" is assembled and encoded'); }