Skip to content

Commit bbb7d3a

Browse files
authored
Delete connection button (#489)
* New button to delete connection to meta assets TODO: Also attempt to clear meta side config. There are some broken tests that are unrelated to this change (some core magento package, and some file formatting that we'll need to address in future)
1 parent bac3b98 commit bbb7d3a

File tree

5 files changed

+233
-57
lines changed

5 files changed

+233
-57
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) Meta Platforms, Inc. and affiliates.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
namespace Meta\BusinessExtension\Block\Adminhtml\System\Config;
22+
23+
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
24+
use Magento\Backend\Block\Template\Context;
25+
use Magento\Backend\Block\Widget\Button;
26+
use Magento\Config\Block\System\Config\Form\Field;
27+
use Magento\Framework\Data\Form\Element\AbstractElement;
28+
29+
class DeleteConnection extends Field
30+
{
31+
/**
32+
* @var string
33+
*/
34+
protected $_template = 'Meta_BusinessExtension::system/config/delete_connection.phtml';
35+
36+
/**
37+
* @var SystemConfig
38+
*/
39+
private $systemConfig;
40+
41+
/**
42+
* @param Context $context
43+
* @param SystemConfig $systemConfig
44+
* @param array $data
45+
*/
46+
public function __construct(
47+
Context $context,
48+
SystemConfig $systemConfig,
49+
array $data = []
50+
) {
51+
$this->systemConfig = $systemConfig;
52+
parent::__construct($context, $data);
53+
}
54+
55+
/**
56+
* Remove scope label
57+
*
58+
* @param AbstractElement $element
59+
* @return string
60+
*/
61+
public function render(AbstractElement $element)
62+
{
63+
$element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
64+
return parent::render($element);
65+
}
66+
67+
/**
68+
* Get ajax URL
69+
*
70+
* @return string
71+
*/
72+
public function getAjaxUrl()
73+
{
74+
return $this->getUrl(
75+
'fbeadmin/ajax/fbdeleteasset',
76+
['storeId' => $this->getRequest()->getParam('store')]
77+
);
78+
}
79+
80+
/**
81+
* Clean cache after deletion connection to Meta assets
82+
*
83+
* @return string
84+
*/
85+
public function getCleanCacheAjaxUrl()
86+
{
87+
return $this->getUrl('fbeadmin/ajax/cleanCache');
88+
}
89+
90+
/**
91+
* Get element html
92+
*
93+
* @param AbstractElement $element
94+
* @return string
95+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
96+
*/
97+
protected function _getElementHtml(AbstractElement $element)
98+
{
99+
return $this->_toHtml();
100+
}
101+
102+
/**
103+
* Get button html
104+
*
105+
* @return string
106+
* @throws \Magento\Framework\Exception\LocalizedException
107+
*/
108+
public function getButtonHtml()
109+
{
110+
/** @var Button $button */
111+
$button = $this->getLayout()->createBlock(Button::class);
112+
return $button->setData(['id' => 'fb_delete_connection_btn', 'label' => __('Delete Connection')])
113+
->toHtml();
114+
}
115+
116+
/**
117+
* Get store id
118+
*
119+
* @return mixed
120+
*/
121+
protected function getStoreId()
122+
{
123+
return $this->getRequest()->getParam('store');
124+
}
125+
}

app/code/Meta/BusinessExtension/Controller/Adminhtml/Ajax/Fbdeleteasset.php

Lines changed: 10 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,28 @@
2020

2121
namespace Meta\BusinessExtension\Controller\Adminhtml\Ajax;
2222

23+
use Magento\Backend\App\Action\Context;
2324
use Magento\Framework\Controller\Result\JsonFactory;
2425
use Magento\Framework\Event\ManagerInterface as EventManager;
25-
use Magento\Framework\Exception\LocalizedException;
2626
use Meta\BusinessExtension\Helper\FBEHelper;
2727
use Magento\Framework\App\Action\HttpDeleteActionInterface;
2828
use Magento\Framework\App\RequestInterface;
29-
use Magento\Security\Model\AdminSessionsManager;
3029
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
3130
use Meta\BusinessExtension\Model\ResourceModel\FacebookInstalledFeature;
3231

33-
class Fbdeleteasset implements HttpDeleteActionInterface
32+
class Fbdeleteasset extends AbstractAjax implements HttpDeleteActionInterface
3433
{
3534
public const DELETE_SUCCESS_MESSAGE = "You have successfully deleted Meta Business Extension." .
3635
" The pixel installed on your website is now deleted.";
3736

3837
private const DELETE_FAILURE_MESSAGE = "There was a problem deleting the connection.
3938
Please try again.";
4039

41-
/**
42-
* @var JsonFactory
43-
*/
44-
private $resultJsonFactory;
45-
4640
/**
4741
* @var FBEHelper
4842
*/
4943
private $fbeHelper;
5044

51-
/**
52-
* @var AdminSessionsManager
53-
*/
54-
private $adminSessionManager;
55-
5645
/**
5746
* @var SystemConfig
5847
*/
@@ -74,58 +63,31 @@ class Fbdeleteasset implements HttpDeleteActionInterface
7463
private EventManager $eventManager;
7564

7665
/**
66+
* @param Context $context
7767
* @param JsonFactory $resultJsonFactory
7868
* @param FBEHelper $fbeHelper
79-
* @param AdminSessionsManager $adminSessionManager
8069
* @param SystemConfig $systemConfig
8170
* @param RequestInterface $request
8271
* @param FacebookInstalledFeature $fbeInstalledFeatureResource
8372
* @param EventManager $eventManager
8473
*/
8574
public function __construct(
75+
Context $context,
8676
JsonFactory $resultJsonFactory,
8777
FBEHelper $fbeHelper,
88-
AdminSessionsManager $adminSessionManager,
8978
SystemConfig $systemConfig,
9079
RequestInterface $request,
9180
FacebookInstalledFeature $fbeInstalledFeatureResource,
9281
EventManager $eventManager
9382
) {
94-
$this->resultJsonFactory = $resultJsonFactory;
83+
parent::__construct($context, $resultJsonFactory, $fbeHelper);
9584
$this->fbeHelper = $fbeHelper;
96-
$this->adminSessionManager = $adminSessionManager;
9785
$this->systemConfig = $systemConfig;
9886
$this->request = $request;
9987
$this->fbeInstalledFeatureResource = $fbeInstalledFeatureResource;
10088
$this->eventManager = $eventManager;
10189
}
10290

103-
/**
104-
* Execute
105-
*
106-
* @throws LocalizedException
107-
*/
108-
public function execute()
109-
{
110-
$result = $this->resultJsonFactory->create();
111-
// TODO : Move all String objects to constants.
112-
$adminSession = $this->adminSessionManager->getCurrentSession();
113-
if (!$adminSession || $adminSession->getStatus() != 1) {
114-
throw new LocalizedException('This endpoint is for logged in admin and ajax only.');
115-
} else {
116-
try {
117-
$json = $this->executeForJson();
118-
return $result->setData($json);
119-
} catch (\Exception $e) {
120-
$this->fbeHelper->logCritical($e->getMessage());
121-
throw new LocalizedException(
122-
'There was error while processing your request.' .
123-
' Please contact admin for more details.'
124-
);
125-
}
126-
}
127-
}
128-
12991
/**
13092
* Execute for json
13193
*
@@ -144,8 +106,8 @@ public function executeForJson()
144106
];
145107
}
146108
try {
147-
$this->deleteConfigKeys($storeId);
148-
$this->deleteInstalledFeatures($storeId);
109+
$this->deleteConfigKeys($storeId)
110+
->deleteInstalledFeatures($storeId);
149111

150112
$this->eventManager->dispatch('facebook_delete_assets_after', ['store_id' => $storeId]);
151113

@@ -176,7 +138,7 @@ public function executeForJson()
176138
* Delete config keys
177139
*
178140
* @param string|int|null $storeId Store ID to delete from.
179-
* @return array
141+
* @return Fbdeleteasset
180142
*/
181143
private function deleteConfigKeys($storeId)
182144
{
@@ -208,9 +170,11 @@ private function deleteConfigKeys($storeId)
208170
* Delete installed features
209171
*
210172
* @param string|int|null $storeId Store ID to delete from.
173+
* @return Fbdeleteasset
211174
*/
212175
private function deleteInstalledFeatures($storeId)
213176
{
214177
$this->fbeInstalledFeatureResource->deleteAll($storeId);
178+
return $this;
215179
}
216180
}

app/code/Meta/BusinessExtension/Test/Unit/Controller/Adminhtml/Ajax/FbdeleteassetTest.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,18 @@
2020

2121
namespace Meta\BusinessExtension\Test\Unit\Controller\Adminhtml\Ajax;
2222

23-
use Magento\Security\Model\AdminSessionsManager;
23+
use Magento\Backend\App\Action\Context;
2424
use Magento\Framework\App\RequestInterface;
2525
use Magento\Framework\Controller\Result\JsonFactory;
2626
use Magento\Framework\Event\ManagerInterface as EventManager;
2727
use Meta\BusinessExtension\Helper\FBEHelper;
2828
use Meta\BusinessExtension\Controller\Adminhtml\Ajax\Fbdeleteasset;
29-
use PHPUnit\Framework\MockObject\MockObject;
3029
use PHPUnit\Framework\TestCase;
3130
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
3231
use Meta\BusinessExtension\Model\ResourceModel\FacebookInstalledFeature;
3332

3433
class FbdeleteassetTest extends TestCase
3534
{
36-
/**
37-
* @var MockObject
38-
*/
39-
private $fbeHelper;
40-
4135
/**
4236
* @var Fbdeleteasset
4337
*/
@@ -74,17 +68,17 @@ public function tearDown(): void
7468
*/
7569
public function setUp(): void
7670
{
71+
$context = $this->createMock(Context::class);
7772
$resultJsonFactory = $this->createMock(JsonFactory::class);
78-
$this->fbeHelper = $this->createMock(FBEHelper::class);
79-
$sessionManager = $this->createMock(AdminSessionsManager::class);
73+
$fbeHelper = $this->createMock(FBEHelper::class);
8074
$this->systemConfig = $this->createMock(SystemConfig::class);
8175
$this->fbeInstalledFeatureResource = $this->createMock(FacebookInstalledFeature::class);
8276
$this->request = $this->createMock(\Magento\Framework\App\RequestInterface::class);
8377
$eventManager = $this->createMock(EventManager::class);
8478
$this->fbdeleteasset = new Fbdeleteasset(
79+
$context,
8580
$resultJsonFactory,
86-
$this->fbeHelper,
87-
$sessionManager,
81+
$fbeHelper,
8882
$this->systemConfig,
8983
$this->request,
9084
$this->fbeInstalledFeatureResource,

app/code/Meta/BusinessExtension/etc/adminhtml/system.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@
8787
<config_path>facebook/debug/debug_mode</config_path>
8888
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
8989
</field>
90+
<field id="delete_connection" translate="label comment" type="select" sortOrder="20" showInDefault="0"
91+
showInWebsite="0" showInStore="1">
92+
<label>Delete Meta Connection</label>
93+
<frontend_model>Meta\BusinessExtension\Block\Adminhtml\System\Config\DeleteConnection</frontend_model>
94+
</field>
9095
</group>
9196
</section>
9297
</system>

0 commit comments

Comments
 (0)