Skip to content

Commit

Permalink
XWIKI-21848: Migrate NotificationFilterPreferenceLivetableResults to …
Browse files Browse the repository at this point in the history
…a Live Data source
  • Loading branch information
surli committed Mar 12, 2024
1 parent a0d806a commit ef4f262
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 538 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.xwiki.notifications.filters.internal.livedata.custom;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -240,7 +241,9 @@ private String getUnstyledList(List<String> items)
private String displayEventTypes(NotificationFilterPreference filterPreference) throws LiveDataException
{
String result;
Set<String> eventTypes = filterPreference.getEventTypes();
List<String> eventTypes = new ArrayList<>(filterPreference.getEventTypes());
// Ensure to always have same order
eventTypes.sort(Comparator.naturalOrder());
if (eventTypes.isEmpty()) {
result = getUnstyledList(List.of(this.translationHelper.getAllEventTypesTranslation()));
} else {
Expand All @@ -256,7 +259,10 @@ private String displayEventTypes(NotificationFilterPreference filterPreference)
private String displayNotificationFormats(NotificationFilterPreference filterPreference)
{
List<String> items = new ArrayList<>();
for (NotificationFormat notificationFormat : filterPreference.getNotificationFormats()) {
List<NotificationFormat> notificationFormats = new ArrayList<>(filterPreference.getNotificationFormats());
// Ensure to always have same order
notificationFormats.sort(Comparator.comparing(NotificationFormat::name));
for (NotificationFormat notificationFormat : notificationFormats) {
items.add(this.translationHelper.getFormatTranslation(notificationFormat));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ private Map<String, String> displayIsEnabledData(NotificationFilter notification
private boolean isEnabled(ToggleableNotificationFilter notificationFilter,
ToggleableNotificationFilterActivation filterActivation)
{
return notificationFilter.isEnabledByDefault() || (filterActivation != null && filterActivation.isEnabled());
return (filterActivation != null && filterActivation.isEnabled()) ||
(filterActivation == null && notificationFilter.isEnabledByDefault());
}

private String displayIsEnabled(ToggleableNotificationFilter notificationFilter,
Expand Down Expand Up @@ -186,7 +187,7 @@ public LiveData get(LiveDataQuery query) throws LiveDataException
.collect(Collectors.toList());
filtersActivations =
this.filterPreferencesModelBridge.getToggleableFilterActivations(
new DocumentReference(wikiReference, NOTIFICATION_ADMINISTRATION_REF));
new DocumentReference(NOTIFICATION_ADMINISTRATION_REF, wikiReference));
}
} else {
XWikiContext context = this.contextProvider.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ void compositeNotifications(TestUtils setup, TestReference testReference,

List<SystemNotificationFilterPreference> minorEvent = p.getSystemNotificationFilterPreferences()
.stream()
.filter(fp -> fp.getFilterName().equals("Minor Event (Alert)"))
.collect(Collectors.toList());
.filter(fp -> fp.getName().equals("Minor Event (Alert)"))
.toList();

assertEquals(1, minorEvent.size());
minorEvent.get(0).setEnabled(false);
Expand Down Expand Up @@ -391,16 +391,18 @@ void ownEventNotifications(TestUtils setup, TestReference testReference) throws

DocumentReference page2 = new DocumentReference("page2", testReference.getLastSpaceReference());
try {
int filterPreferenceNumber = 4;
NotificationsUserProfilePage p = NotificationsUserProfilePage.gotoPage(FIRST_USER_NAME);
List<SystemNotificationFilterPreference> preferences = p.getSystemNotificationFilterPreferences();

// Now let's do some changes (own even filter)
SystemNotificationFilterPreference filterPreference = preferences.get(filterPreferenceNumber);
p.setApplicationState(SYSTEM, "alert", BootstrapSwitch.State.ON);
assertEquals("Own Events Filter", preferences.get(2).getFilterName());
preferences.get(2).setEnabled(false);
assertEquals("Own Events Filter", filterPreference.getName());
filterPreference.setEnabled(false);
setup.gotoPage(page2);
p = NotificationsUserProfilePage.gotoPage(FIRST_USER_NAME);
assertFalse(p.getSystemNotificationFilterPreferences().get(2).isEnabled());
assertFalse(p.getSystemNotificationFilterPreferences().get(filterPreferenceNumber).isEnabled());

// Watch the entire wiki so that we receive notifications
NotificationsTrayPage tray = new NotificationsTrayPage();
Expand All @@ -419,9 +421,10 @@ void ownEventNotifications(TestUtils setup, TestReference testReference) throws
// Go back to enable the own even filter
p = NotificationsUserProfilePage.gotoPage(FIRST_USER_NAME);
preferences = p.getSystemNotificationFilterPreferences();
assertEquals("Own Events Filter", preferences.get(2).getFilterName());
assertFalse(preferences.get(2).isEnabled());
preferences.get(2).setEnabled(true);
filterPreference = preferences.get(filterPreferenceNumber);
assertEquals("Own Events Filter", filterPreference.getName());
assertFalse(filterPreference.isEnabled());
filterPreference.setEnabled(true);
setup.createPage(page2, "", "Page 2");
setup.gotoPage(testReference.getLastSpaceReference().getName(), testReference.getName());
notificationsTrayPage = new NotificationsTrayPage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,42 +198,48 @@ void notificationFiltersDefaultValues(TestUtils testUtils)
assertEquals(6, preferences.size());

// Filter 0
assertEquals("Minor Event (Alert)", preferences.get(0).getFilterName());
assertEquals("Hide notifications concerning minor changes on pages", preferences.get(0).getDescription());
assertEquals(List.of("Alert"), preferences.get(0).getFormats());
assertTrue(preferences.get(0).isEnabled());
SystemNotificationFilterPreference filter0 = preferences.get(0);
assertEquals("Read Event Filter (Alert)", filter0.getName());
assertEquals("Hide notifications that you have marked as read", filter0.getDescription());
assertEquals(List.of("Alert"), filter0.getFormats());
assertFalse(filter0.isEnabled());

// Filter 1
assertEquals("Minor Event (Email)", preferences.get(1).getFilterName());
assertEquals("Hide notifications concerning minor changes on pages", preferences.get(1).getDescription());
assertEquals(List.of("Email"), preferences.get(1).getFormats());
assertTrue(preferences.get(1).isEnabled());
SystemNotificationFilterPreference filter1 = preferences.get(1);
assertEquals("Read Event Filter (Email)", filter1.getName());
assertEquals("Hide notifications that you have marked as read", filter1.getDescription());
assertEquals(List.of("Email"), filter1.getFormats());
assertFalse(filter1.isEnabled());

// Filter 2
assertEquals("Own Events Filter", preferences.get(2).getFilterName());
assertEquals("Hide notifications about your own activity unless the event specifically targets you",
preferences.get(2).getDescription());
assertEquals(List.of("Alert", "Email"), preferences.get(2).getFormats());
assertTrue(preferences.get(2).isEnabled());
SystemNotificationFilterPreference filter2 = preferences.get(2);
assertEquals("Minor Event (Alert)", filter2.getName());
assertEquals("Hide notifications concerning minor changes on pages", filter2.getDescription());
assertEquals(List.of("Alert"), filter2.getFormats());
assertTrue(filter2.isEnabled());

// Filter 3
assertEquals("Read Event Filter (Alert)", preferences.get(3).getFilterName());
assertEquals("Hide notifications that you have marked as read", preferences.get(3).getDescription());
assertEquals(List.of("Alert"), preferences.get(3).getFormats());
assertFalse(preferences.get(3).isEnabled());
SystemNotificationFilterPreference filter3 = preferences.get(3);
assertEquals("Minor Event (Email)", filter3.getName());
assertEquals("Hide notifications concerning minor changes on pages", filter3.getDescription());
assertEquals(List.of("Email"), filter3.getFormats());
assertTrue(filter3.isEnabled());

// Filter 4
assertEquals("Read Event Filter (Email)", preferences.get(4).getFilterName());
assertEquals("Hide notifications that you have marked as read", preferences.get(4).getDescription());
assertEquals(List.of("Email"), preferences.get(4).getFormats());
assertFalse(preferences.get(4).isEnabled());
SystemNotificationFilterPreference filter4 = preferences.get(4);
assertEquals("Own Events Filter", filter4.getName());
assertEquals("Hide notifications about your own activity unless the event specifically targets you",
filter4.getDescription());
assertEquals(List.of("Alert", "Email"), filter4.getFormats());
assertTrue(filter4.isEnabled());

// Filter 5
assertEquals("System Filter", preferences.get(5).getFilterName());
SystemNotificationFilterPreference filter5 = preferences.get(5);
assertEquals("System Filter", filter5.getName());
assertEquals("Hide notifications from the System user unless the event specifically targets you",
preferences.get(5).getDescription());
assertEquals(List.of("Alert", "Email"), preferences.get(5).getFormats());
assertTrue(preferences.get(5).isEnabled());
filter5.getDescription());
assertEquals(List.of("Alert", "Email"), filter5.getFormats());
assertTrue(filter5.isEnabled());
}

@Test
Expand Down Expand Up @@ -263,7 +269,7 @@ void filterAndWatchedPage(TestUtils testUtils, TestReference testReference) thro
assertEquals(1, preferences.size());

// Filter 0
assertTrue(preferences.get(0).getFilterName().contains("Page only"));
assertEquals("Page only", preferences.get(0).getScope());
assertEquals(testReference.getLastSpaceReference().getName() + ".WebHome",
preferences.get(0).getLocation());
assertEquals(CustomNotificationFilterPreference.FilterAction.NOTIFY_EVENT,
Expand Down Expand Up @@ -303,7 +309,7 @@ void filterAndWatchedPage(TestUtils testUtils, TestReference testReference) thro
assertEquals(1, preferences.size());

// Filter 1
assertTrue(preferences.get(0).getFilterName().contains("Page and children"));
assertEquals("Page and children", preferences.get(0).getScope());
assertEquals(testReference.getLastSpaceReference().getName() + ".WebHome",
preferences.get(0).getLocation());
assertEquals(CustomNotificationFilterPreference.FilterAction.NOTIFY_EVENT,
Expand All @@ -330,7 +336,7 @@ void filterAndWatchedPage(TestUtils testUtils, TestReference testReference) thro
assertEquals(2, preferences.size());

// Filter 2
assertTrue(preferences.get(1).getFilterName().contains("Page only"));
assertEquals("Page only", preferences.get(1).getScope());
assertEquals(testReference.getLastSpaceReference().getName() + "." + testReference.getName(),
preferences.get(1).getLocation());
assertEquals(CustomNotificationFilterPreference.FilterAction.IGNORE_EVENT,
Expand Down Expand Up @@ -604,7 +610,7 @@ void addCustomFilters(TestUtils testUtils)
CustomNotificationFilterPreference filterPreference =
customNotificationFilterPreferences.get(0);

assertTrue(filterPreference.getFilterName().contains("Page and children"));
assertEquals("Page and children", filterPreference.getScope());
assertEquals(NotificationsSettingsIT.class.getSimpleName() + ".WebHome", filterPreference.getLocation());
assertEquals(CustomNotificationFilterPreference.FilterAction.NOTIFY_EVENT, filterPreference.getFilterAction());
assertEquals(List.of("Email"), filterPreference.getFormats());
Expand All @@ -625,7 +631,7 @@ void addCustomFilters(TestUtils testUtils)
assertEquals(1, customNotificationFilterPreferences.size());
filterPreference = customNotificationFilterPreferences.get(0);

assertTrue(filterPreference.getFilterName().contains("Page and children"));
assertEquals("Page and children", filterPreference.getScope());
assertEquals(NotificationsSettingsIT.class.getSimpleName() + ".WebHome", filterPreference.getLocation());
assertEquals(CustomNotificationFilterPreference.FilterAction.NOTIFY_EVENT, filterPreference.getFilterAction());
assertEquals(List.of("Email"), filterPreference.getFormats());
Expand All @@ -648,8 +654,7 @@ void addCustomFilters(TestUtils testUtils)

filterPreference = customNotificationFilterPreferences.get(1);

assertTrue(filterPreference.getFilterName().contains("Page"));
assertFalse(filterPreference.getFilterName().contains("Page and children"));
assertEquals("Page only", filterPreference.getScope());
assertEquals(NotificationsSettingsIT.class.getSimpleName() + ".SubSpace.SubPage",
filterPreference.getLocation());
assertEquals(CustomNotificationFilterPreference.FilterAction.IGNORE_EVENT, filterPreference.getFilterAction());
Expand All @@ -669,16 +674,15 @@ void addCustomFilters(TestUtils testUtils)
filterPreference =
customNotificationFilterPreferences.get(0);

assertTrue(filterPreference.getFilterName().contains("Page and children"));
assertEquals("Page and children", filterPreference.getScope());
assertEquals(NotificationsSettingsIT.class.getSimpleName() + ".WebHome", filterPreference.getLocation());
assertEquals(CustomNotificationFilterPreference.FilterAction.NOTIFY_EVENT, filterPreference.getFilterAction());
assertEquals(List.of("Email"), filterPreference.getFormats());
assertEquals(List.of("A page is modified"), filterPreference.getEventTypes());

filterPreference = customNotificationFilterPreferences.get(1);

assertTrue(filterPreference.getFilterName().contains("Page"));
assertFalse(filterPreference.getFilterName().contains("Page and children"));
assertEquals("Page only", filterPreference.getScope());
assertEquals(NotificationsSettingsIT.class.getSimpleName() + ".SubSpace.SubPage",
filterPreference.getLocation());
assertEquals(CustomNotificationFilterPreference.FilterAction.IGNORE_EVENT, filterPreference.getFilterAction());
Expand All @@ -705,21 +709,21 @@ void addCustomFilters(TestUtils testUtils)

filterPreference = customNotificationFilterPreferences.get(2);

assertTrue(filterPreference.getFilterName().contains("Page"));
assertFalse(filterPreference.getFilterName().contains("Page and children"));
assertEquals("Page only", filterPreference.getScope());
assertEquals(NotificationsSettingsIT.class.getSimpleName() + ".Page1", filterPreference.getLocation());
assertEquals(CustomNotificationFilterPreference.FilterAction.NOTIFY_EVENT, filterPreference.getFilterAction());
assertEquals(List.of("Alert"), filterPreference.getFormats());
assertEquals(List.of("A page is modified", "A page is deleted"), filterPreference.getEventTypes());
// core.events.delete before core.events.update
assertEquals(List.of("A page is deleted", "A page is modified"), filterPreference.getEventTypes());

filterPreference = customNotificationFilterPreferences.get(3);

assertTrue(filterPreference.getFilterName().contains("Page"));
assertFalse(filterPreference.getFilterName().contains("Page and children"));
assertEquals("Page only", filterPreference.getScope());
assertEquals(NotificationsSettingsIT.class.getSimpleName() + ".Page2", filterPreference.getLocation());
assertEquals(CustomNotificationFilterPreference.FilterAction.NOTIFY_EVENT, filterPreference.getFilterAction());
assertEquals(List.of("Alert"), filterPreference.getFormats());
assertEquals(List.of("A page is modified", "A page is deleted"), filterPreference.getEventTypes());
// core.events.delete before core.events.update
assertEquals(List.of("A page is deleted","A page is modified"), filterPreference.getEventTypes());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ public abstract class AbstractNotificationFilterPreference

private WebElement row;

private String filterName;

private List<String> formats = new ArrayList<>();

private BootstrapSwitch enabledSwitch;
Expand All @@ -64,7 +62,6 @@ public AbstractNotificationFilterPreference(AbstractNotificationsSettingsPage pa
{
this.parentPage = parentPage;
this.row = row;
this.filterName = getNameElement(row).getText();
List<WebElement> formatElements = getFormatsElement(row)
.findElements(By.tagName("li"));
for (WebElement format : formatElements) {
Expand All @@ -74,13 +71,6 @@ public AbstractNotificationFilterPreference(AbstractNotificationsSettingsPage pa
this.enabledSwitch = new BootstrapSwitch(getBootstrapSwitchElement(row), webDriver);
}

/**
* @param row the row to get the name from
* @return the {@link WebElement} containing the name of the row
* @since 16.1.0RC1
*/
protected abstract WebElement getNameElement(WebElement row);

/**
* @param row the row to get the formats from
* @return the {@link WebElement} containing the formats of the row
Expand All @@ -100,14 +90,6 @@ private WebElement getBootstrapSwitchElement(WebElement row)
return row.findElement(By.className("displayer-toggle")).findElement(By.className("bootstrap-switch"));
}

/**
* @return the filter name.
*/
public String getFilterName()
{
return filterName;
}

/**
* @return the parent page where the settings are displayed.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,12 @@ public CustomNotificationFilterPreference(AbstractNotificationsSettingsPage pare
}
}

@Override
protected WebElement getNameElement(WebElement row)
/**
* @return the scope of the filter.
*/
public String getScope()
{
return row.findElement(By.cssSelector("td[data-title='Location']"));
return getRow().findElement(By.cssSelector("td[data-title='Scope']")).getText();
}

/**
Expand All @@ -110,7 +112,7 @@ public List<String> getEventTypes()
*/
public String getLocation()
{
return this.getRow().findElement(By.cssSelector("td[data-title='Location'] .html-wrapper ol"))
return getRow().findElement(By.cssSelector("td[data-title='Location'] .html-wrapper ol"))
.getAttribute("data-entity");
}

Expand Down
Loading

0 comments on commit ef4f262

Please sign in to comment.