From 29c3fad7113ff263dd51fef45d033ac4673bab03 Mon Sep 17 00:00:00 2001
From: Claus Due <claus@namelesscoder.net>
Date: Thu, 17 Nov 2016 13:14:24 +0100
Subject: [PATCH] [BUGFIX] Make showAccessRestrictedPages work

This patch fixes a problem where setting showAccessRestrictedPages
setting in HMENU etc. did not result in the correct behavior according
to documentation at https://docs.typo3.org/typo3cms/TyposcriptReference/MenuObjects/CommonProperties/Index.html.

The patch changes the behavior caused by using `NONE` as
keyword in the setting.

Before: using the option renders `<a>Page title</a>` when page is inaccessible.

After: using the option renders `<a href="index.php?id=123">Page title</a>`
when page is not accessible.

Change-Id: I4364f7ec9f44e1e0b70654830efc236f20e6bf30
Resolves: #17904
Releases: master, 7.6
Reviewed-on: https://review.typo3.org/50696
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Jan Helke <typo3@helke.de>
Tested-by: Jan Helke <typo3@helke.de>
---
 ...rictedPagesDoesNotWorkWithSpecialMenus.rst |  19 ++
 .../Menu/AbstractMenuContentObject.php        |   4 +-
 .../Menu/AbstractMenuContentObjectTest.php    | 249 ++++++++++++++++++
 3 files changed, 270 insertions(+), 2 deletions(-)
 create mode 100644 typo3/sysext/core/Documentation/Changelog/master/Important-17904-ShowAccessRestrictedPagesDoesNotWorkWithSpecialMenus.rst

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Important-17904-ShowAccessRestrictedPagesDoesNotWorkWithSpecialMenus.rst b/typo3/sysext/core/Documentation/Changelog/master/Important-17904-ShowAccessRestrictedPagesDoesNotWorkWithSpecialMenus.rst
new file mode 100644
index 000000000000..537ffc049d9f
--- /dev/null
+++ b/typo3/sysext/core/Documentation/Changelog/master/Important-17904-ShowAccessRestrictedPagesDoesNotWorkWithSpecialMenus.rst
@@ -0,0 +1,19 @@
+.. include:: ../../Includes.txt
+
+==============================================================================
+Important: #17904 - showAccessRestrictedPages does not work with special menus
+==============================================================================
+
+See :issue:`17904`
+
+Description
+===========
+
+HMENU setting `showAccessRestrictedPages`=NONE now acts as documented in https://docs.typo3.org/typo3cms/TyposcriptReference/MenuObjects/CommonProperties/Index.html.
+
+Before: using the option renders `<a>Page title</a>` when page is inaccessible.
+
+After: using the option renders `<a href="index.php?id=123">Page title</a>`
+when page is not accessible.
+
+.. index:: Frontend, TypoScript
\ No newline at end of file
diff --git a/typo3/sysext/frontend/Classes/ContentObject/Menu/AbstractMenuContentObject.php b/typo3/sysext/frontend/Classes/ContentObject/Menu/AbstractMenuContentObject.php
index 21d5d742fd25..b393990278b6 100644
--- a/typo3/sysext/frontend/Classes/ContentObject/Menu/AbstractMenuContentObject.php
+++ b/typo3/sysext/frontend/Classes/ContentObject/Menu/AbstractMenuContentObject.php
@@ -1627,7 +1627,7 @@ abstract class AbstractMenuContentObject
             $LD['totalURL'] = $this->parent_cObj->typoLink_URL([
                 'parameter' => $shortcut['uid'],
                 'additionalParams' => $addParams . $this->I['val']['additionalParams'] . $menuItem['_ADD_GETVARS'],
-                'linkAccessRestrictedPages' => $this->mconf['showAccessRestrictedPages'] && $this->mconf['showAccessRestrictedPages'] !== 'NONE'
+                'linkAccessRestrictedPages' => !empty($this->mconf['showAccessRestrictedPages'])
             ]);
         }
         if ($shortcut) {
@@ -2128,7 +2128,7 @@ abstract class AbstractMenuContentObject
         if ($page['sectionIndex_uid']) {
             $conf['section'] = $page['sectionIndex_uid'];
         }
-        $conf['linkAccessRestrictedPages'] = $this->mconf['showAccessRestrictedPages'] && $this->mconf['showAccessRestrictedPages'] !== 'NONE';
+        $conf['linkAccessRestrictedPages'] = !empty($this->mconf['showAccessRestrictedPages']);
         $this->parent_cObj->typoLink('|', $conf);
         $LD = $this->parent_cObj->lastTypoLinkLD;
         $LD['totalURL'] = $this->parent_cObj->lastTypoLinkUrl;
diff --git a/typo3/sysext/frontend/Tests/Unit/ContentObject/Menu/AbstractMenuContentObjectTest.php b/typo3/sysext/frontend/Tests/Unit/ContentObject/Menu/AbstractMenuContentObjectTest.php
index 8c51e926930b..48afb14b12f6 100644
--- a/typo3/sysext/frontend/Tests/Unit/ContentObject/Menu/AbstractMenuContentObjectTest.php
+++ b/typo3/sysext/frontend/Tests/Unit/ContentObject/Menu/AbstractMenuContentObjectTest.php
@@ -292,4 +292,253 @@ class AbstractMenuContentObjectTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
 
         $this->assertEquals($expectedResult, $this->subject->isItemState('IFSUB', 0));
     }
+
+    /**
+     * @return array
+     */
+    public function menuTypoLinkCreatesExpectedTypoLinkConfiurationDataProvider()
+    {
+        return [
+            'standard parameter without access protected setting' => [
+                [
+                    'parameter' => 1,
+                    'linkAccessRestrictedPages' => false,
+                    'useCacheHash' => true
+                ],
+                [
+                    'showAccessRestrictedPages' => false
+                ],
+                true,
+                ['uid' => 1],
+                '',
+                0,
+                '',
+                '',
+                '',
+                ''
+            ],
+            'standard parameter with access protected setting' => [
+                [
+                    'parameter' => 10,
+                    'linkAccessRestrictedPages' => true,
+                    'useCacheHash' => true
+                ],
+                [
+                    'showAccessRestrictedPages' => true
+                ],
+                true,
+                ['uid' => 10],
+                '',
+                0,
+                '',
+                '',
+                '',
+                ''
+            ],
+            'standard parameter with access protected setting "NONE" casts to boolean linkAccessRestrictedPages (delegates resolving to typoLink method internals)' => [
+                [
+                    'parameter' => 10,
+                    'linkAccessRestrictedPages' => true,
+                    'useCacheHash' => true
+                ],
+                [
+                    'showAccessRestrictedPages' => 'NONE'
+                ],
+                true,
+                ['uid' => 10],
+                '',
+                0,
+                '',
+                '',
+                '',
+                ''
+            ],
+            'standard parameter with access protected setting (int)67 casts to boolean linkAccessRestrictedPages (delegates resolving to typoLink method internals)' => [
+                [
+                    'parameter' => 10,
+                    'linkAccessRestrictedPages' => true,
+                    'useCacheHash' => true
+                ],
+                [
+                    'showAccessRestrictedPages' => 67
+                ],
+                true,
+                ['uid' => 10],
+                '',
+                0,
+                '',
+                '',
+                '',
+                ''
+            ],
+            'standard parameter with target' => [
+                [
+                    'parameter' => 1,
+                    'target' => '_blank',
+                    'linkAccessRestrictedPages' => false,
+                    'useCacheHash' => true
+                ],
+                [
+                    'showAccessRestrictedPages' => false
+                ],
+                true,
+                ['uid' => 1],
+                '_blank',
+                0,
+                '',
+                '',
+                '',
+                ''
+            ],
+            'parameter with typeOverride=10' => [
+                [
+                    'parameter' => '10,10',
+                    'linkAccessRestrictedPages' => false,
+                    'useCacheHash' => true
+                ],
+                [
+                    'showAccessRestrictedPages' => false
+                ],
+                true,
+                ['uid' => 10],
+                '',
+                0,
+                '',
+                '',
+                '',
+                10
+            ],
+            'parameter with target and typeOverride=10' => [
+                [
+                    'parameter' => '10,10',
+                    'linkAccessRestrictedPages' => false,
+                    'useCacheHash' => true,
+                    'target' => '_self'
+                ],
+                [
+                    'showAccessRestrictedPages' => false
+                ],
+                true,
+                ['uid' => 10],
+                '_self',
+                0,
+                '',
+                '',
+                '',
+                10
+            ],
+            'parameter with invalid value in typeOverride=foobar ignores typeOverride' => [
+                [
+                    'parameter' => 20,
+                    'linkAccessRestrictedPages' => false,
+                    'useCacheHash' => true,
+                    'target' => '_self'
+                ],
+                [
+                    'showAccessRestrictedPages' => false
+                ],
+                true,
+                ['uid' => 20],
+                '_self',
+                0,
+                '',
+                '',
+                '',
+                'foobar'
+            ],
+            'standard parameter with section name' => [
+                [
+                    'parameter' => 10,
+                    'target' => '_blank',
+                    'linkAccessRestrictedPages' => false,
+                    'no_cache' => true,
+                    'section' => 'section-name'
+                ],
+                [
+                    'showAccessRestrictedPages' => false
+                ],
+                true,
+                [
+                    'uid' => 10,
+                    'sectionIndex_uid' => 'section-name'
+                ],
+                '_blank',
+                1,
+                '',
+                '',
+                '',
+                ''
+            ],
+            'standard parameter with additional parameters' => [
+                [
+                    'parameter' => 10,
+                    'linkAccessRestrictedPages' => false,
+                    'no_cache' => true,
+                    'section' => 'section-name',
+                    'additionalParams' => '&test=foobar'
+                ],
+                [
+                    'showAccessRestrictedPages' => false
+                ],
+                true,
+                [
+                    'uid' => 10,
+                    'sectionIndex_uid' => 'section-name'
+                ],
+                '',
+                1,
+                '',
+                '',
+                '&test=foobar',
+                ''
+            ],
+            'overridden page array uid value gets used as parameter' => [
+                [
+                    'parameter' => 99,
+                    'linkAccessRestrictedPages' => false,
+                    'no_cache' => true,
+                    'section' => 'section-name'
+                ],
+                [
+                    'showAccessRestrictedPages' => false
+                ],
+                true,
+                [
+                    'uid' => 10,
+                    'sectionIndex_uid' => 'section-name'
+                ],
+                '',
+                1,
+                '',
+                ['uid' => 99],
+                '',
+                ''
+            ],
+        ];
+    }
+
+    /**
+     * @test
+     * @dataProvider menuTypoLinkCreatesExpectedTypoLinkConfiurationDataProvider
+     * @param array $expected
+     * @param array $mconf
+     * @param bool $useCacheHash
+     * @param array $page
+     * @param mixed $oTarget
+     * @param int $no_cache
+     * @param string $script
+     * @param string $overrideArray
+     * @param string $addParams
+     * @param string $typeOverride
+     */
+    public function menuTypoLinkCreatesExpectedTypoLinkConfiguration(array $expected, array $mconf, $useCacheHash = true, array $page, $oTarget, $no_cache, $script, $overrideArray = '', $addParams = '', $typeOverride = '')
+    {
+        $this->subject->parent_cObj = $this->getMockBuilder(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class)
+            ->setMethods(['typoLink'])
+            ->getMock();
+        $this->subject->mconf = $mconf;
+        $this->subject->_set('useCacheHash', $useCacheHash);
+        $this->subject->parent_cObj->expects($this->once())->method('typoLink')->with('|', $expected);
+        $this->subject->menuTypoLink($page, $oTarget, $no_cache, $script, $overrideArray, $addParams, $typeOverride);
+    }
 }
-- 
GitLab