Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
piwik_pro
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
piwik_pro
Commits
47474129
Commit
47474129
authored
2 weeks ago
by
dimas11
Committed by
Heikki Ylipaavalniemi
2 weeks ago
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3496921
by dimas11, heikkiy: Improve test coverage for role visibility
parent
4c0e4d08
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!15
Snippet visibility test cases added. Mode: Selected only; Selected: empty.
Pipeline
#388320
passed
2 weeks ago
Stage: build
Stage: validate
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/PiwikProSnippet.php
+49
-33
49 additions, 33 deletions
src/PiwikProSnippet.php
tests/src/Functional/PiwikProSnippetTest.php
+107
-3
107 additions, 3 deletions
tests/src/Functional/PiwikProSnippetTest.php
with
156 additions
and
36 deletions
src/PiwikProSnippet.php
+
49
−
33
View file @
47474129
...
...
@@ -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
;
}
}
This diff is collapsed.
Click to expand it.
tests/src/Functional/PiwikProSnippetTest.php
+
107
−
3
View file @
47474129
...
...
@@ -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
();
// No
w no
t 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.
*/
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment