Skip to content

Commit d02574e

Browse files
committed
B2B-2206: [Spike] Find All Areas in GraphQL That Break When Session is Disabled
1 parent c26c7e7 commit d02574e

File tree

4 files changed

+202
-8
lines changed

4 files changed

+202
-8
lines changed

app/code/Magento/GraphQl/Model/Config/DisableSession.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ public function isDisabled($scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT
4444
$scopeCode
4545
);
4646

47-
if ($value === null) {
48-
return false;
47+
if ($value === '1') {
48+
return true;
4949
}
5050

51-
return (bool)$value;
51+
return false;
5252
}
5353
}

app/code/Magento/GraphQl/Plugin/DisableSession.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@ public function __construct(
5151
*/
5252
public function afterCheck(SessionStartChecker $subject, bool $result): bool
5353
{
54+
if (!$result) {
55+
return false;
56+
}
5457
try {
55-
if (!$result ||
56-
($this->appState->getAreaCode() === Area::AREA_GRAPHQL && $this->disableSessionConfig->isDisabled())
57-
) {
58-
return false;
58+
if ($this->appState->getAreaCode() === Area::AREA_GRAPHQL && $this->disableSessionConfig->isDisabled()) {
59+
$result = false;
5960
}
6061
} catch (LocalizedException $e) {
61-
//@codingStandardsIgnoreLine
62+
$result = false;
6263
} finally {
6364
return $result;
6465
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Test\Unit\Model\Config;
9+
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
12+
use Magento\GraphQl\Model\Config\DisableSession;
13+
use PHPUnit\Framework\MockObject\MockObject;
14+
use PHPUnit\Framework\TestCase;
15+
16+
/**
17+
* Test for DisableSession config model.
18+
*/
19+
class DisableSessionTest extends TestCase
20+
{
21+
/**
22+
* @var ScopeConfigInterface|MockObject
23+
*/
24+
private $scopeConfigMock;
25+
26+
/**
27+
* @var DisableSession
28+
*/
29+
private $model;
30+
31+
/**
32+
* @inheirtDoc
33+
*/
34+
public function setUp(): void
35+
{
36+
$this->scopeConfigMock = $this->getMockForAbstractClass(ScopeConfigInterface::class);
37+
$this->model = (new ObjectManager($this))->getObject(
38+
DisableSession::class,
39+
['scopeConfig' => $this->scopeConfigMock]
40+
);
41+
}
42+
43+
/**
44+
* @dataProvider disableSessionDataProvider
45+
*/
46+
public function testisSessionDisabled($configValue, $expectedResult)
47+
{
48+
$this->scopeConfigMock->expects($this->any())->method('getValue')->willReturn($configValue);
49+
$this->assertEquals($expectedResult, $this->model->isDisabled());
50+
}
51+
52+
/**
53+
* Data provider for session disabled config test.
54+
* @return array[]
55+
*/
56+
public function disableSessionDataProvider()
57+
{
58+
return [
59+
['configValue' => '1', true],
60+
['configValue' => '0', false],
61+
['configValue' => '11', false],
62+
['configValue' => null, false],
63+
['configValue' => '', false],
64+
['configValue' => 'adfjsadf', false],
65+
];
66+
}
67+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Test\Unit\Plugin;
9+
10+
use Magento\Framework\App\State;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\Session\SessionStartChecker;
13+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
14+
use Magento\GraphQl\Model\Config\DisableSession;
15+
use Magento\GraphQl\Plugin\DisableSession as DisableSessionPlugin;
16+
use PHPUnit\Framework\MockObject\MockObject;
17+
use PHPUnit\Framework\TestCase;
18+
19+
/**
20+
* Test for DisableSession plugin.
21+
*/
22+
class DisableSessionTest extends TestCase
23+
{
24+
/**
25+
* @var DisableSession|MockObject
26+
*/
27+
private $disableSessionConfigMock;
28+
29+
/**
30+
* @var State|MockObject
31+
*/
32+
private $appStateMock;
33+
34+
/**
35+
* @var DisableSessionPlugin
36+
*/
37+
private $model;
38+
39+
/**
40+
* @var SessionStartChecker|MockObject
41+
*/
42+
private $sessionStartCheckerMock;
43+
44+
public function setUp(): void
45+
{
46+
$this->disableSessionConfigMock = $this->createMock(DisableSession::class);
47+
$this->appStateMock = $this->createMock(State::class);
48+
$this->sessionStartCheckerMock = $this->createMock(SessionStartChecker::class);
49+
$this->model = (new ObjectManager($this))->getObject(
50+
DisableSessionPlugin::class,
51+
[
52+
'disableSessionConfig' => $this->disableSessionConfigMock,
53+
'appState' => $this->appStateMock
54+
]
55+
);
56+
}
57+
58+
59+
/**
60+
* Test afterCheck plugin result over original method result.
61+
*
62+
* @param string $area
63+
* @param bool $config
64+
* @param bool $methodResult
65+
* @param bool $expectedResult
66+
* @return void
67+
* @dataProvider testAfterCheckDataProvider
68+
*/
69+
public function testAfterCheck(string $area, bool $config, bool $methodResult, bool $expectedResult)
70+
{
71+
$this->disableSessionConfigMock->expects($this->any())->method('isDisabled')->willReturn($config);
72+
$this->appStateMock->expects($this->any())->method('getAreaCode')->willReturn($area);
73+
$this->assertEquals($expectedResult, $this->model->afterCheck($this->sessionStartCheckerMock, $methodResult));
74+
}
75+
76+
/**
77+
* Data provider for testAfterCheck.
78+
*
79+
* @return array[]
80+
*/
81+
public function testAfterCheckDataProvider()
82+
{
83+
return [
84+
['area' => 'graphql', 'config' => true, 'methodResult' => false, 'expected' => false],
85+
['area' => 'graphql', 'config' => true, 'methodResult' => true, 'expected' => false],
86+
['area' => 'graphql', 'config' => false, 'methodResult' => true, 'expected' => true],
87+
['area' => 'graphql', 'config' => false, 'methodResult' => false, 'expected' => false],
88+
['area' => 'other', 'config' => false, 'methodResult' => false, 'expected' => false],
89+
['area' => 'other', 'config' => true, 'methodResult' => false, 'expected' => false],
90+
['area' => 'other', 'config' => true, 'methodResult' => true, 'expected' => true],
91+
['area' => 'other', 'config' => false, 'methodResult' => true, 'expected' => true],
92+
];
93+
}
94+
95+
/**
96+
* Test afterCheck plugin result over original method result when no area code set.
97+
*
98+
* @param bool $config
99+
* @param bool $methodResult
100+
* @param bool $expectedResult
101+
* @return void
102+
* @dataProvider testAfterCheckDataProviderNoAreaCode
103+
*/
104+
public function testAfterCheckNoArea(bool $config, bool $methodResult, bool $expectedResult)
105+
{
106+
$this->appStateMock->expects($this->any())
107+
->method('getAreaCode')
108+
->willThrowException(new LocalizedException(__('Are code not set')));
109+
$this->assertEquals($expectedResult, $this->model->afterCheck($this->sessionStartCheckerMock, $methodResult));
110+
}
111+
112+
/**
113+
* Data provider for testAfterCheck.
114+
*
115+
* @return array[]
116+
*/
117+
public function testAfterCheckDataProviderNoAreaCode()
118+
{
119+
return [
120+
['config' => true, 'methodResult' => true, 'expected' => false],
121+
['config' => true, 'methodResult' => false, 'expected' => false],
122+
['config' => false, 'methodResult' => true, 'expected' => false],
123+
['config' => false, 'methodResult' => false, 'expected' => false],
124+
];
125+
}
126+
}

0 commit comments

Comments
 (0)