Skip to content

Commit

Permalink
Merge pull request #602 from paulandrieux/bugfix/496-548-template-wid…
Browse files Browse the repository at this point in the history
…getmaps

Fix for widget overwrites and deletions
  • Loading branch information
paulandrieux authored Sep 9, 2016
2 parents d47552a + 1ede46a commit 9b0b5d4
Show file tree
Hide file tree
Showing 7 changed files with 447 additions and 98 deletions.
6 changes: 4 additions & 2 deletions Bundle/WidgetBundle/Entity/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,12 @@ public function setChildrenSlot($childrenSlot)
*
* @return Widget
*/
public function setWidgetMap(WidgetMap $widgetMap)
public function setWidgetMap(WidgetMap $widgetMap = null)
{
if ($widgetMap) {
$widgetMap->addWidget($this);
}
$this->widgetMap = $widgetMap;
$widgetMap->addWidget($this);

return $this;
}
Expand Down
16 changes: 4 additions & 12 deletions Bundle/WidgetBundle/Model/WidgetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,33 +344,25 @@ public function deleteWidget(Widget $widget, View $view)
public function overwriteWidget(View $view, Widget $widget)
{
$widgetCopy = $this->cloneEntity($widget);

//we have to persist the widget to get its id
$this->entityManager->persist($view);
$this->entityManager->flush();

$originalWidgetMap = WidgetMapHelper::getWidgetMapByWidgetAndView($widget, $view);

$originalWidgetMap = $widget->getWidgetMap();
$this->widgetMapManager->overwrite($view, $originalWidgetMap, $widgetCopy);

$this->widgetMapBuilder->build($view);

return $widgetCopy;
}

/**
* @param Widget $entity
*/
public function cloneEntity($entity)
public function cloneEntity(Widget $entity)
{
$entityCopy = clone $entity;

$entityCopy->setWidgetMap(null);
//Look for on_to_many relations, if found, duplicate related entities.
//It is necessary for 'list' widgets, this algo duplicates and persists list items.
$associations = $this->entityManager->getClassMetadata(get_class($entityCopy))->getAssociationMappings();
$accessor = PropertyAccess::createPropertyAccessor();
foreach ($associations as $name => $values) {
if ($values['type'] === ClassMetadataInfo::ONE_TO_MANY && $values['fieldName'] != 'widgetMaps') {
if ($values['type'] === ClassMetadataInfo::ONE_TO_MANY) {
$relatedEntities = $accessor->getValue($entityCopy, $values['fieldName']);
$relatedEntitiesCopies = [];
foreach ($relatedEntities as $relatedEntity) {
Expand Down
10 changes: 8 additions & 2 deletions Bundle/WidgetBundle/Resolver/WidgetResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,15 @@ public function __construct(DataSourceChain $dataSourceChain, AuthorizationCheck

public function resolve(WidgetMap $widgetMap)
{
//TODO: orderiaze it
//TODO: orderize it

$widgets = $widgetMap->getWidgets();
// if the widgetmap is linked to no widgets, it seems that it is an overwrite of the position so keep the replaced widgets for display
if ($widgetMap->getReplaced() && count($widgets) === 0) {
$widgets = $widgetMap->getReplaced()->getWidgets();
}
/* @var Widget $widget */
foreach ($widgetMap->getWidgets() as $_widget) {
foreach ($widgets as $_widget) {
/** @var Criteria $criteria */
foreach ($_widget->getCriterias() as $criteria) {
$value = $this->dataSourceChain->getData($criteria->getName());
Expand Down
20 changes: 14 additions & 6 deletions Bundle/WidgetMapBundle/Manager/WidgetMapManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ public function move(View $view, $sortedWidget)
$this->moveChildren($view, $beforeChild, $afterChild, $originalParent, $originalPosition);

foreach ($parentWidgetMapChildren['views'] as $_view) {
if ($_view->getId() !== $view->getId()) {
if (isset($parentWidgetMapChildren['before'][$_view->getId()])) {
if ($_view !== $view) {
if (isset($parentWidgetMapChildren['before'][$_view->getId()]) && $parentWidgetMapChildren['before'][$_view->getId()]->getPosition() == $widgetMap->getPosition()) {
$parentWidgetMapChildren['before'][$_view->getId()]->setParent($widgetMap);
}
if (isset($parentWidgetMapChildren['after'][$_view->getId()])) {
if (isset($parentWidgetMapChildren['after'][$_view->getId()]) && $parentWidgetMapChildren['after'][$_view->getId()]->getPosition() == $widgetMap->getPosition()) {
$parentWidgetMapChildren['after'][$_view->getId()]->setParent($widgetMap);
}
}
Expand Down Expand Up @@ -121,6 +121,17 @@ public function delete(View $view, Widget $widget)

//we remove the widget from the current view
if ($widgetMap->getView() === $view) {
// If the widgetMap has substitutes, delete them or transform them in create mode
if (count($widgetMap->getSubstitutes()) > 0) {
foreach ($widgetMap->getSubstitutes() as $substitute) {
if ($substitute->getAction() === WidgetMap::ACTION_OVERWRITE) {
$substitute->setAction(WidgetMap::ACTION_CREATE);
$substitute->setReplaced(null);
} else {
$view->removeWidgetMap($widgetMap);
}
}
}
//remove the widget map from the slot
$view->removeWidgetMap($widgetMap);
} else {
Expand Down Expand Up @@ -154,10 +165,8 @@ public function overwrite(View $view, WidgetMap $originalWidgetMap, Widget $widg
$widgetMap->setAction(WidgetMap::ACTION_OVERWRITE);
$widgetMap->setReplaced($originalWidgetMap);
$widgetCopy->setWidgetMap($widgetMap);
$widgetMap->setView($view);
$widgetMap->setSlot($originalWidgetMap->getSlot());
$widgetMap->setPosition($originalWidgetMap->getPosition());
$widgetMap->setAsynchronous($widgetCopy->isAsynchronous());
$widgetMap->setParent($originalWidgetMap->getParent());

$view->addWidgetMap($widgetMap);
Expand Down Expand Up @@ -209,7 +218,6 @@ protected function cloneWidgetMap(WidgetMap $widgetMap, View $view)
$widgetMap->setId(null);
$widgetMap->setAction(WidgetMap::ACTION_OVERWRITE);
$widgetMap->setReplaced($originalWidgetMap);
$originalWidgetMap->addSubstitute($widgetMap);
$widgetMap->setView($view);
$view->addWidgetMap($widgetMap);
$this->em->persist($widgetMap);
Expand Down
6 changes: 4 additions & 2 deletions Tests/Features/Context/VictoireContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,11 @@ public function shouldPrecedeForTheQuery($textBefore, $textAfter)
{
$element = $this->getSession()->getPage()->find(
'xpath',
sprintf('//descendant-or-self::*[normalize-space(text()) = "%s"]/ancestor::div/descendant-or-self::*[normalize-space(text()) = "%s"]', $textBefore, $textAfter)
sprintf('//*[normalize-space(text()) = "%s"][preceding::*[normalize-space(text()) = "%s"]]',
$textAfter,
$textBefore
)
);

if (null === $element) {
$message = sprintf('"%s" does not preceed "%s"', $textBefore, $textAfter);
throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession());
Expand Down
146 changes: 72 additions & 74 deletions Tests/Features/WidgetMap/widgetMap.feature
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
@mink:selenium2 @alice(Page) @reset-schema
@mink:selenium2 @alice(Page) @alice(Template) @reset-schema
Feature: Test widgetMap
# Ececuted tests:
# On a simple page:
# - add
# - delete
# - move

Background:
Given I maximize the window
And I am on homepage
@reset-schema
Scenario: I move up a widget
Scenario: I move first a widget from simple page
Given the following WidgetMaps:
| id | action | position | parent | slot | view |
| 1 | create | | | main_content | home |
Expand All @@ -16,15 +21,15 @@ Scenario: I move up a widget
| Widget 1 | static | 1 |
| Widget 2 | static | 2 |
| Widget 3 | static | 3 |
And I am on the homepage
Then I should see "Widget 1"
When I move the widgetMap "1" "before" the widgetMap "3"
And I wait 2 seconds
And I reload the page
And "Widget 1" should precede "Widget 3"
And I am on the homepage
Then I should see "Widget 1"
When I move the widgetMap "3" "before" the widgetMap "1"
And I wait 2 seconds
And I reload the page
And "Widget 3" should precede "Widget 1"

@reset-schema
Scenario: I move first a widget
Scenario: I move up a widget from simple page
Given the following WidgetMaps:
| id | action | position | parent | slot | view |
| 1 | create | | | main_content | home |
Expand All @@ -35,15 +40,15 @@ Scenario: I move first a widget
| Widget 1 | static | 1 |
| Widget 2 | static | 2 |
| Widget 3 | static | 3 |
And I am on the homepage
Then I should see "Widget 1"
When I move the widgetMap "3" "after" the widgetMap ""
And I wait 2 seconds
And I reload the page
And "Widget 3" should precede "Widget 1"
And I am on the homepage
Then I should see "Widget 1"
When I move the widgetMap "1" "before" the widgetMap "3"
And I wait 2 seconds
And I reload the page
And "Widget 1" should precede "Widget 3"

@reset-schema
Scenario: I move down a widget
Scenario: I move down a widget from simple page
Given the following WidgetMaps:
| id | action | position | parent | slot | view |
| 1 | create | | | main_content | home |
Expand All @@ -54,16 +59,50 @@ Scenario: I move down a widget
| Widget 1 | static | 1 |
| Widget 2 | static | 2 |
| Widget 3 | static | 3 |
And I am on the homepage
Then I should see "Widget 1"
When I move the widgetMap "1" "after" the widgetMap "2"
And I wait 2 seconds
And I reload the page
Then "Widget 2" should precede "Widget 1"
And I am on the homepage
Then I should see "Widget 1"
When I move the widgetMap "1" "after" the widgetMap "2"
And I wait 2 seconds
And I reload the page
Then "Widget 2" should precede "Widget 1"

@reset-schema
Scenario: I move a widget under a templates one
Scenario: I add widget in a position from simple page
Then I switch to "layout" mode
Then I should see "Nouveau Contenu"
When I select "Force" from the "1" select of "main_content" slot
Then I should see "Créer"
When I fill in "Côté de la force" with "obscur"
And I submit the widget
And I wait 2 seconds
And I should see "Le côté obscur de la force"

Then I should see "Nouveau Contenu"
When I select "Force" from the "2" select of "main_content" slot
Then I should see "Créer"
When I fill in "Côté de la force" with "Lumineux"
And I submit the widget
Then I should see "Le côté Lumineux de la force"
And "Le côté obscur de la force" should precede "Le côté Lumineux de la force"

Given I reload the page
Then "Le côté obscur de la force" should precede "Le côté Lumineux de la force"

Then I should see "Nouveau Contenu"
Given I select "Force" from the "2" select of "main_content" slot
Then I should see "Créer"
When I fill in "Côté de la force" with "Double"
And I submit the widget
Then I should see "Le côté Double de la force"
And "Le côté Double de la force" should precede "Le côté Lumineux de la force"
And "Le côté obscur de la force" should precede "Le côté Double de la force"

Given I reload the page
And "Le côté Double de la force" should precede "Le côté Lumineux de la force"
And "Le côté obscur de la force" should precede "Le côté Double de la force"

@reset-schema
Scenario: I delete widget from simple page
Given the following WidgetMaps:
| id | action | position | parent | slot | view |
| 1 | create | | | main_content | home |
Expand All @@ -74,54 +113,13 @@ Scenario: I move a widget under a templates one
| Widget 1 | static | 1 |
| Widget 2 | static | 2 |
| Widget 3 | static | 3 |
And I am on the homepage
Then I should see "Widget 1"
When I switch to "layout" mode
Then I should see "Nouveau Contenu"
When I select "Texte brut" from the "3" select of "main_content" slot
Then I should see "Créer"
When I fill in "Texte *" with "Widget 4"
And I submit the widget
And I reload the page
And "Widget 2" should precede "Widget 4"
And "Widget 4" should precede "Widget 3"
Then I move the widgetMap "1" "after" the widgetMap "4"
And I wait 2 seconds
And I reload the page
Then "Widget 4" should precede "Widget 1"
Then "Widget 1" should precede "Widget 3"

@reset-schema
Scenario: I create widget in a position
Then I switch to "layout" mode
Then I should see "Nouveau Contenu"
When I select "Force" from the "1" select of "main_content" slot
Then I should see "Créer"
When I fill in "Côté de la force" with "obscur"
And I submit the widget
And I wait 2 seconds
And I should see "Le côté obscur de la force"

Then I should see "Nouveau Contenu"
When I select "Force" from the "2" select of "main_content" slot
Then I should see "Créer"
When I fill in "Côté de la force" with "Lumineux"
And I submit the widget
Then I should see "Le côté Lumineux de la force"
And "Le côté obscur de la force" should precede "Le côté Lumineux de la force"

Given I reload the page
Then "Le côté obscur de la force" should precede "Le côté Lumineux de la force"

Then I should see "Nouveau Contenu"
Given I select "Force" from the "2" select of "main_content" slot
Then I should see "Créer"
When I fill in "Côté de la force" with "Double"
And I submit the widget
Then I should see "Le côté Double de la force"
And "Le côté Double de la force" should precede "Le côté Lumineux de la force"
And "Le côté obscur de la force" should precede "Le côté Double de la force"

Given I reload the page
And "Le côté Double de la force" should precede "Le côté Lumineux de la force"
And "Le côté obscur de la force" should precede "Le côté Double de la force"
And I am on the homepage
Then I should see "Widget 1"
When I switch to "edit" mode
And I edit the "Text" widget
Then I should see "Supprimer"
Given I follow "Supprimer"
Then I should see "Cette action va définitivement supprimer ce contenu. Cette action est irréversible. Êtes-vous sûr ?"
Given I press "J'ai bien compris, je confirme la suppression"
And I reload the page
And "Widget 3" should precede "Widget 2"
Loading

0 comments on commit 9b0b5d4

Please sign in to comment.