|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Customer\Test\Unit\Model\Plugin; |
| 7 | + |
| 8 | +use Magento\Customer\Model\Plugin\CustomerFlushFormKey; |
| 9 | +use Magento\Customer\Model\Session; |
| 10 | +use Magento\Framework\App\PageCache\FormKey as CookieFormKey; |
| 11 | +use Magento\Framework\Data\Form\FormKey as DataFormKey; |
| 12 | +use Magento\Framework\Event\Observer; |
| 13 | +use Magento\PageCache\Observer\FlushFormKey; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use PHPUnit_Framework_MockObject_MockObject as MockObject; |
| 16 | + |
| 17 | +class CustomerFlushFormKeyTest extends TestCase |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var CookieFormKey | MockObject |
| 21 | + */ |
| 22 | + private $cookieFormKey; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var Session | MockObject |
| 26 | + */ |
| 27 | + private $customerSession; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var DataFormKey | MockObject |
| 31 | + */ |
| 32 | + private $dataFormKey; |
| 33 | + |
| 34 | + protected function setUp() |
| 35 | + { |
| 36 | + |
| 37 | + /** @var CookieFormKey | MockObject */ |
| 38 | + $this->cookieFormKey = $this->getMockBuilder(CookieFormKey::class) |
| 39 | + ->disableOriginalConstructor() |
| 40 | + ->getMock(); |
| 41 | + |
| 42 | + /** @var DataFormKey | MockObject */ |
| 43 | + $this->dataFormKey = $this->getMockBuilder(DataFormKey::class) |
| 44 | + ->disableOriginalConstructor() |
| 45 | + ->getMock(); |
| 46 | + |
| 47 | + /** @var Session | MockObject */ |
| 48 | + $this->customerSession = $this->getMockBuilder(Session::class) |
| 49 | + ->disableOriginalConstructor() |
| 50 | + ->setMethods(['getBeforeRequestParams', 'setBeforeRequestParams']) |
| 51 | + ->getMock(); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @dataProvider aroundFlushFormKeyProvider |
| 56 | + * @param $beforeFormKey |
| 57 | + * @param $currentFormKey |
| 58 | + * @param $getFormKeyTimes |
| 59 | + * @param $setBeforeParamsTimes |
| 60 | + */ |
| 61 | + public function testAroundFlushFormKey( |
| 62 | + $beforeFormKey, |
| 63 | + $currentFormKey, |
| 64 | + $getFormKeyTimes, |
| 65 | + $setBeforeParamsTimes |
| 66 | + ) { |
| 67 | + $observerDto = new Observer(); |
| 68 | + $observer = new FlushFormKey($this->cookieFormKey, $this->dataFormKey); |
| 69 | + $plugin = new CustomerFlushFormKey($this->customerSession, $this->dataFormKey); |
| 70 | + |
| 71 | + $beforeParams['form_key'] = $beforeFormKey; |
| 72 | + |
| 73 | + $this->dataFormKey->expects($this->exactly($getFormKeyTimes)) |
| 74 | + ->method('getFormKey') |
| 75 | + ->willReturn($currentFormKey); |
| 76 | + |
| 77 | + $this->customerSession->expects($this->once()) |
| 78 | + ->method('getBeforeRequestParams') |
| 79 | + ->willReturn($beforeParams); |
| 80 | + |
| 81 | + $this->customerSession->expects($this->exactly($setBeforeParamsTimes)) |
| 82 | + ->method('setBeforeRequestParams') |
| 83 | + ->with($beforeParams); |
| 84 | + |
| 85 | + $proceed = function ($observerDto) use ($observer) { |
| 86 | + return $observer->execute($observerDto); |
| 87 | + }; |
| 88 | + |
| 89 | + $plugin->aroundExecute($observer, $proceed, $observerDto); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Data provider for testAroundFlushFormKey |
| 94 | + * |
| 95 | + * @return array |
| 96 | + */ |
| 97 | + public function aroundFlushFormKeyProvider() |
| 98 | + { |
| 99 | + return [ |
| 100 | + ['form_key_value', 'form_key_value', 2, 1], |
| 101 | + ['form_old_key_value', 'form_key_value', 1, 0], |
| 102 | + [null, 'form_key_value', 1, 0] |
| 103 | + ]; |
| 104 | + } |
| 105 | +} |
0 commit comments