Skip to content
Snippets Groups Projects
Commit 47474129 authored by dimas11's avatar dimas11 Committed by Heikki Ylipaavalniemi
Browse files

Issue #3496921 by dimas11, heikkiy: Improve test coverage for role visibility

parent 4c0e4d08
No related branches found
No related tags found
1 merge request!15Snippet visibility test cases added. Mode: Selected only; Selected: empty.
Pipeline #388320 passed
......@@ -147,35 +147,38 @@ tags.async=!0,tags.src="%s"+id+".js"+qPString,scripts.parentNode.insertBefore(ta
* Check if the snippet should be shown on this page (TRUE) or not (FALSE).
*/
public function getVisibilityPages(): bool {
static $page_match;
static $is_visible;
// Cache visibility result if function is called more than once.
if (!isset($page_match)) {
$visibility_request_path_mode = $this->config->get('visibility.request_path_mode');
$visibility_request_path_pages = (string) $this->config->get('visibility.request_path_pages');
// Match path if necessary.
if (!empty($visibility_request_path_pages)) {
$pages = mb_strtolower($visibility_request_path_pages);
if ($visibility_request_path_mode < 2) {
$path = $this->currentPath->getPath();
$path_alias = mb_strtolower($this->aliasManager->getAliasByPath($path));
$page_match = $this->pathMatcher->matchPath($path_alias, $pages) || (($path != $path_alias) && $this->pathMatcher->matchPath($path, $pages));
// When $visibility_request_path_mode has a value of 0, the tracking
// code is displayed on all pages except those listed in $pages. When
// set to 1, it is displayed only on those pages listed in $pages.
$page_match = !($visibility_request_path_mode xor $page_match);
}
else {
$page_match = FALSE;
}
if (isset($is_visible)) {
return $is_visible;
}
$visibility_request_path_mode = $this->config->get('visibility.request_path_mode');
$visibility_request_path_pages = mb_strtolower((string) $this->config->get('visibility.request_path_pages'));
$path = $this->currentPath->getPath();
$path_alias = mb_strtolower($this->aliasManager->getAliasByPath($path));
$page_match = $this->pathMatcher->matchPath($path_alias, $visibility_request_path_pages) || (($path != $path_alias) && $this->pathMatcher->matchPath($path, $visibility_request_path_pages));
if ($visibility_request_path_mode === 1) {
// The listed pages only.
if ($page_match) {
return $is_visible = TRUE;
}
else {
$page_match = TRUE;
return $is_visible = FALSE;
}
}
else {
// Every page except the listed pages.
if ($page_match) {
return $is_visible = FALSE;
}
else {
return $is_visible = TRUE;
}
}
return $page_match;
}
/**
......@@ -185,22 +188,35 @@ tags.async=!0,tags.src="%s"+id+".js"+qPString,scripts.parentNode.insertBefore(ta
* user (TRUE) or not (FALSE).
*/
public function getVisibilityRoles(): bool {
static $role_match;
static $is_visible;
// Cache visibility result if function is called more than once.
if (!isset($role_match)) {
$visibility_user_roles = $this->config->get('visibility.user_roles');
if (empty($visibility_user_roles)) {
$role_match = TRUE;
if (isset($is_visible)) {
return $is_visible;
}
$visibility_user_roles = $this->config->get('visibility.user_roles');
$visibility_user_role_mode = $this->config->get('visibility.user_role_mode');
$role_matches = array_intersect($this->currentUser->getRoles(), $visibility_user_roles);
if ($visibility_user_role_mode === 1) {
// The listed roles only.
if ($role_matches) {
return $is_visible = TRUE;
}
else {
$visibility_user_role_mode = $this->config->get('visibility.user_role_mode');
$matches = array_intersect($this->currentUser->getRoles(), $visibility_user_roles);
$role_match = $visibility_user_role_mode === 0 ? empty($matches) : !empty($matches);
return $is_visible = FALSE;
}
}
else {
// Every role except the listed roles.
if ($role_matches) {
return $is_visible = FALSE;
}
else {
return $is_visible = TRUE;
}
}
return $role_match;
}
}
......@@ -122,20 +122,78 @@ class PiwikProSnippetTest extends BrowserTestBase {
$this->assertSession()->responseNotContains((string) $this->config->get('piwik_domain'));
}
/**
* Test if the snippet shows on "Every page except the listed pages" selected.
*/
public function testSnippetVisibilityPages(): void {
$this->config->set('visibility.request_path_mode', 0)
->save();
$this->config->set('visibility.request_path_pages', '/admin/modules')
->save();
$this->refreshVariables();
// Shown on the front page.
$this->drupalGet('');
$this->assertSession()->responseContains((string) $this->config->get('piwik_domain'));
// But does not show on admin/modules.
$this->drupalGet('admin/modules');
$this->assertSession()->responseNotContains((string) $this->config->get('piwik_domain'));
}
/**
* Test if the snippet shows with setting "Show only on these pages".
*/
public function testSnippetVisibilityPagesInverted(): void {
$this->config->set('visibility.request_path_mode', 1)->save();
$this->config->set('visibility.request_path_mode', 1)
->save();
$this->config->set('visibility.request_path_pages', '/admin/modules')
->save();
$this->refreshVariables();
// Now not shown on the front page.
// Not shown on the front page.
$this->drupalGet('');
$this->assertSession()->responseNotContains((string) $this->config->get('piwik_domain'));
// But does show on admin/modules.
// Show on admin/modules.
$this->drupalGet('admin/modules');
$this->assertSession()->responseContains((string) $this->config->get('piwik_domain'));
}
/**
* Test if the snippet shows on "Every page except the listed pages" selected.
*
* No pages entered.
*/
public function testSnippetVisibilityPagesNoPagesEntered(): void {
$this->config->set('visibility.request_path_mode', 0)
->save();
$this->config->set('visibility.request_path_pages', '')
->save();
$this->refreshVariables();
// Shown on the front page.
$this->drupalGet('');
$this->assertSession()->responseContains((string) $this->config->get('piwik_domain'));
// Show on admin/modules.
$this->drupalGet('admin/modules');
$this->assertSession()->responseContains((string) $this->config->get('piwik_domain'));
}
/**
* Test if the snippet shows on "Show only on these pages" selected.
*
* No pages entered.
*/
public function testSnippetVisibilityPagesNoPagesEnteredInverted(): void {
$this->config->set('visibility.request_path_mode', 1)
->save();
$this->config->set('visibility.request_path_pages', '')
->save();
$this->refreshVariables();
// Does not show on the front page.
$this->drupalGet('');
$this->assertSession()->responseNotContains((string) $this->config->get('piwik_domain'));
// Does not show on admin/modules.
$this->drupalGet('admin/modules');
$this->assertSession()->responseNotContains((string) $this->config->get('piwik_domain'));
}
/**
* Test if the snippet doesn't show for the listed roles.
*/
......@@ -178,6 +236,52 @@ class PiwikProSnippetTest extends BrowserTestBase {
$this->assertSession()->responseContains((string) $this->config->get('piwik_domain'));
}
/**
* Test if the snippet is shown.
*
* When mode is 'Every role except the listed roles' and no roles selected.
*/
public function testSnippetVisibilityRolesNoRoleSelected(): void {
$this->config
->set('visibility.user_role_mode', 0)
->set('visibility.user_roles', [])
->save();
$this->refreshVariables();
// Now shown on the front page as authenticated user.
$this->drupalGet('');
$this->assertSession()->responseContains((string) $this->config->get('piwik_domain'));
$this->drupalLogout();
// Shown on the front page as anonymous user.
$this->drupalGet('');
$this->assertSession()->responseContains((string) $this->config->get('piwik_domain'));
}
/**
* Test if the snippet does not show.
*
* When mode is 'The listed roles only' and no roles selected.
*/
public function testSnippetVisibilityRolesNoRoleSelectedInverted(): void {
$this->config
->set('visibility.user_role_mode', 1)
->set('visibility.user_roles', [])
->save();
$this->refreshVariables();
// Not shown on the front page as authenticated user.
$this->drupalGet('');
$this->assertSession()->responseNotContains((string) $this->config->get('piwik_domain'));
$this->drupalLogout();
// Not shown on the front page as anonymous user.
$this->drupalGet('');
$this->assertSession()->responseNotContains((string) $this->config->get('piwik_domain'));
}
/**
* Test if the datalayer is changed when set in config.
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment