Uploaded image for project: 'Magnolia UI'
  1. Magnolia UI
  2. MGNLUI-7248

AbstractAvailabilityRule: faulty check of multiple items when negate is true

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Neutral Neutral
    • 6.2.24
    • 6.2.19
    • None
    • Yes
    • Nucleus 18
    • 2

      The logic implemented in info.magnolia.ui.availability.rule.AbstractAvailabilityRule#isAvailable is wrong, when testing multiple items and AvailabilityRuleDefinition#getNegate returns true. Consider the following unit test, the last assertion should not fail.

      @Test
      public void testAvailabilityRuleForMultipleItems() {
          ConfiguredAvailabilityDefinition availabilityDefinition = new ConfiguredAvailabilityDefinition();
          availabilityDefinition.setNodeTypes(List.of(NodeTypes.Page.NAME));
      
          JcrNodeTypeRuleDefinition isPageDefinition = new JcrNodeTypeRuleDefinition();
          JcrNodeTypeRule isPage = new JcrNodeTypeRule(availabilityDefinition, isPageDefinition);
      
          JcrNodeTypeRuleDefinition notPageDefinition = new JcrNodeTypeRuleDefinition();
          notPageDefinition.setNegate(true);
          JcrNodeTypeRule isNotPage = new JcrNodeTypeRule(availabilityDefinition, notPageDefinition);
      
          MockNode page = new MockNode("page", NodeTypes.Page.NAME);
          Collection<Node> listOfPages = List.of(page, page);
          Assert.assertTrue(isPage.isAvailable(listOfPages));
          Assert.assertFalse(isNotPage.isAvailable(listOfPages)); 
      
          MockNode area = new MockNode("area", NodeTypes.Component.NAME);
          Collection<Node> listOfAreas = List.of(area, area);
          Assert.assertFalse(isPage.isAvailable(listOfAreas));
          Assert.assertTrue(isNotPage.isAvailable(listOfAreas));;
      
          Collection<Node> listOfPagesAndAreas = List.of(page, area);
          Assert.assertFalse(isPage.isAvailable(listOfPagesAndAreas));
          Assert.assertFalse(isNotPage.isAvailable(listOfPagesAndAreas)); // FAILS, but isAvailable should return false
      } 
      
      
      

      Consider the following implementation as a possible solution

      info.magnolia.ui.availability.rule.AbstractAvailabilityRule
      @Override
      public boolean isAvailable(Collection<?> items) {
          if (shouldByPass(items)) {
              return true;
          }
          
          for (Object item : items) {
              if (getRuleDefinition().getNegate() == isAvailableForItem(item)) {
                  return false;
              }
          }
      
          return true;
      } 
      

        Acceptance criteria

              asiska Adam Siska
              pguettler Philipp Guettler
              Nucleus
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

                Created:
                Updated:
                Resolved: