Skip to content

Commit bcaac77

Browse files
Merge pull request #572 from Smartling/WP-946-file-deletion-audit-log
add audit log entry on file removal (WP-946)
2 parents b7af3b3 + 0f9c8bb commit bcaac77

File tree

11 files changed

+119
-19
lines changed

11 files changed

+119
-19
lines changed

inc/Smartling/Extensions/Acf/AcfDynamicSupport.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ private function getBlogListForSearch(): array
9494
foreach ($profiles as $profile) {
9595
if (
9696
($profile instanceof ConfigurationProfileEntity)
97-
&& in_array($profile->getOriginalBlogId()->getBlogId(), $blogs, true)
97+
&& in_array($profile->getSourceLocale()->getBlogId(), $blogs, true)
9898
) {
99-
$blogsToSearch[] = $profile->getOriginalBlogId()->getBlogId();
99+
$blogsToSearch[] = $profile->getSourceLocale()->getBlogId();
100100
}
101101
}
102102

inc/Smartling/Services/BlogRemovalHandler.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Smartling\Settings\SettingsManager;
99
use Smartling\Submissions\SubmissionEntity;
1010
use Smartling\Submissions\SubmissionManager;
11+
use Smartling\Vendor\Smartling\AuditLog\Params\CreateRecordParameters;
1112
use Smartling\WP\WPHookInterface;
1213

1314
/** @noinspection PhpUnused */
@@ -38,6 +39,14 @@ public function register(): void
3839
public function blogRemovalHandler(int $blogId): void
3940
{
4041
$submissions = $this->getSubmissions($blogId);
42+
$profiles = [];
43+
foreach ($this->settingsManager->getEntities() as $profile) {
44+
$array = [];
45+
foreach ($profile->getTargetLocales() as $locale) {
46+
$array[$locale->getBlogId()] = $profile;
47+
}
48+
$profiles[$profile->getSourceLocale()->getBlogId()] = $array;
49+
}
4150

4251
if (0 < count($submissions)) {
4352
$this->getLogger()->info(
@@ -61,13 +70,22 @@ public function blogRemovalHandler(int $blogId): void
6170
'File %s is not in use and will be deleted', [$submission->getFileUri()]
6271
)
6372
);
73+
$profile = $profiles[$submission->getSourceBlogId()][$submission->getTargetBlogId()] ?? null;
74+
if ($profile !== null) {
75+
$this->apiWrapper->createAuditLogRecord(
76+
$profile,
77+
CreateRecordParameters::ACTION_TYPE_DELETE,
78+
"Blog deletion handler, submissionId={$submission->getId()}, fileUri={$submission->getFileUri()}",
79+
[],
80+
);
81+
}
6482
$this->apiWrapper->deleteFile($submission);
6583
}
6684
}
6785
}
6886

6987
foreach ($this->settingsManager->getEntities() as $profile) {
70-
if ($profile->getOriginalBlogId()->getBlogId() === $blogId) {
88+
if ($profile->getSourceLocale()->getBlogId() === $blogId) {
7189
$this->settingsManager->deleteProfile($profile->getId());
7290
$this->getLogger()->notice("Deleted profile profileId={$profile->getId()} while deleting blogId=$blogId");
7391
} else {

inc/Smartling/Settings/ConfigurationProfileEntity.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,12 @@ public function setIsActive(int $isActive): void
186186
$this->stateFields['is_active'] = $isActive;
187187
}
188188

189-
public function getOriginalBlogId(): Locale
189+
public function getSourceLocale(): Locale
190190
{
191191
return $this->stateFields['original_blog_id'] ?? new Locale();
192192
}
193193

194-
public function setLocale(Locale $mainLocale): void
194+
public function setSourceLocale(Locale $mainLocale): void
195195
{
196196
$this->stateFields['original_blog_id'] = $mainLocale;
197197
}
@@ -204,7 +204,7 @@ public function setOriginalBlogId(int $blogId): void
204204
{
205205
$locale = new Locale();
206206
$locale->setBlogId($blogId);
207-
$this->setLocale($locale);
207+
$this->setSourceLocale($locale);
208208
}
209209

210210
public function getAutoAuthorize(): bool
@@ -398,7 +398,7 @@ public function toArray($addVirtualColumns = true): array
398398
{
399399
$state = parent::toArray(false);
400400

401-
$state['original_blog_id'] = $this->getOriginalBlogId()->getBlogId();
401+
$state['original_blog_id'] = $this->getSourceLocale()->getBlogId();
402402

403403
$state['auto_authorize'] = !$state['auto_authorize'] ? 0 : 1;
404404
$state['is_active'] = !$state['is_active'] ? 0 : 1;

inc/Smartling/Settings/SettingsManager.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,14 +295,14 @@ public function deleteProfile(int $id): void
295295

296296
protected function updateLabels(ConfigurationProfileEntity $entity): ConfigurationProfileEntity
297297
{
298-
$mainLocaleBlogId = $entity->getOriginalBlogId()->getBlogId();
298+
$mainLocaleBlogId = $entity->getSourceLocale()->getBlogId();
299299
if (0 < $mainLocaleBlogId) {
300300
try {
301-
$entity->getOriginalBlogId()->setLabel($this->getSiteHelper()
301+
$entity->getSourceLocale()->setLabel($this->getSiteHelper()
302302
->getBlogLabelById($this->getPluginProxy(), $mainLocaleBlogId));
303303
} catch (BlogNotFoundException $e) {
304304
$this->getLogger()->notice("Got {$e->getMessage()}, removing profileId={$entity->getId()}");
305-
$entity->getOriginalBlogId()->setLabel("* deleted blog *");
305+
$entity->getSourceLocale()->setLabel("* deleted blog *");
306306
$this->deleteProfile($entity->getId());
307307
}
308308
}

inc/Smartling/WP/Controller/ConfigurationProfileFormController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public function save(): void
212212
$locale->setBlogId($defaultBlogId);
213213
$locale->setLabel($this->siteHelper->getBlogLabelById($this->localizationPluginProxy, $defaultBlogId));
214214

215-
$profile->setLocale($locale);
215+
$profile->setSourceLocale($locale);
216216
}
217217

218218
$usedTargetLocales = [];

inc/Smartling/WP/Table/BulkSubmitTableWidget.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public function processBulkAction(): void
231231
foreach ($submissions as $submission) {
232232
[$id] = explode('-', $submission);
233233
$type = $this->getContentTypeFilterValue();
234-
$curBlogId = $this->getProfile()->getOriginalBlogId()->getBlogId();
234+
$curBlogId = $this->getProfile()->getSourceLocale()->getBlogId();
235235
foreach ($locales as $blogId => $blogName) {
236236
$submissionId = $this->core->prepareForUpload(
237237
$type,

inc/Smartling/WP/View/ConfigurationProfileForm.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@
432432
<?php
433433
$tagOptions = ['prompt' => __('Please select source locale', $domain)];
434434
$options = HtmlTagGeneratorHelper::renderSelectOptions(
435-
$profile->getOriginalBlogId()->getBlogId(),
435+
$profile->getSourceLocale()->getBlogId(),
436436
$locales,
437437
$tagOptions
438438
);
@@ -447,7 +447,7 @@
447447
<?php else: ?>
448448
<p>
449449
<?= __('Site source language is: ', $domain) ?>
450-
<strong><?= $profile->getOriginalBlogId()->getLabel() ?></strong>
450+
<strong><?= $profile->getSourceLocale()->getLabel() ?></strong>
451451
</p>
452452
<p>
453453
<a href="#" id="change-default-locale"><?= __('Change source locale', $domain) ?></a>
@@ -456,7 +456,7 @@
456456
<?= HtmlTagGeneratorHelper::tag(
457457
'select',
458458
HtmlTagGeneratorHelper::renderSelectOptions(
459-
$profile->getOriginalBlogId()->getBlogId(),
459+
$profile->getSourceLocale()->getBlogId(),
460460
$locales
461461
),
462462
['name' => 'smartling_settings[defaultLocale]',
@@ -473,7 +473,7 @@
473473
<?php
474474
$targetLocales = $profile->getTargetLocales();
475475
foreach ($locales as $blogId => $label) {
476-
if ($blogId === $profile->getOriginalBlogId()
476+
if ($blogId === $profile->getSourceLocale()
477477
->getBlogId()
478478
) {
479479
continue;

plugin-examples/mlp3-migrate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
$siteHelper = new SiteHelper();
2828
$settingsManager = new SettingsManager($db, $pageSize, $siteHelper, $localizationProxy);
2929
$profile = ArrayHelper::first($settingsManager->getActiveProfiles());
30-
$sourceBlogId = $profile->getOriginalBlogId()->getBlogId();
30+
$sourceBlogId = $profile->getSourceLocale()->getBlogId();
3131
$slug = 'migrate_mlp3_data_to_smartling';
3232
$title = 'Migrate MLP3 data to Smartling';
3333
if (($_POST['action'] ?? '') === 'Migrate' && wp_verify_nonce($_POST['nonce'] ?? '', $slug)) {

tests/IntegrationTests/SmartlingUnitTestCaseAbstract.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ protected function createProfile(): ConfigurationProfileEntity
286286
$locale->setBlogId(1);
287287
$locale->setLabel('');
288288

289-
$profile->setLocale($locale);
289+
$profile->setSourceLocale($locale);
290290
$profile->setAutoAuthorize(1);
291291
$profile->setRetrievalType('pseudo');
292292
$profile->setUploadOnUpdate(1);
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace Smartling\Tests\Services;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Smartling\Services\BlogRemovalHandler;
7+
use Smartling\Settings\ConfigurationProfileEntity;
8+
use Smartling\Settings\TargetLocale;
9+
use Smartling\Submissions\SubmissionEntity;
10+
use Smartling\Vendor\Smartling\AuditLog\Params\CreateRecordParameters;
11+
use Smartling\ApiWrapperInterface;
12+
use Smartling\Settings\SettingsManager;
13+
use Smartling\Submissions\SubmissionManager;
14+
15+
class BlogRemovalHandlerTest extends TestCase
16+
{
17+
public function testBlogRemovalHandler()
18+
{
19+
$locales = [];
20+
for ($blogId = 1; $blogId < 4; $blogId++) {
21+
$locale = $this->createMock(TargetLocale::class);
22+
$locale->method('getBlogId')->willReturn($blogId);
23+
$locales[] = $locale;
24+
}
25+
26+
$profiles = [];
27+
foreach ([1, 3] as $blogId) {
28+
$profile = $this->createMock(ConfigurationProfileEntity::class);
29+
$profile->method('getSourceLocale')->willReturn(array_values(array_filter($locales, static function (TargetLocale $locale) use ($blogId) {
30+
return $locale->getBlogId() === $blogId;
31+
}))[0]);
32+
$profile->method('getTargetLocales')->willReturn(array_values(array_filter($locales, static function (TargetLocale $locale) use ($blogId) {
33+
return $locale->getBlogId() !== $blogId;
34+
})));
35+
$profiles[] = $profile;
36+
}
37+
38+
$settingsManager = $this->createMock(SettingsManager::class);
39+
$settingsManager->method('getEntities')->willReturn($profiles);
40+
41+
$submissions = [];
42+
foreach (['12', '13', '31', '32'] as $blogs) {
43+
$submission = $this->createMock(SubmissionEntity::class);
44+
$submission->method('getFileUri')->willReturn($blogs);
45+
$submission->method('getId')->willReturn((int)$blogs);
46+
$submission->method('getSourceBlogId')->willReturn((int)$blogs[0]);
47+
$submission->method('getTargetBlogId')->willReturn((int)$blogs[1]);
48+
$submissions[] = $submission;
49+
}
50+
51+
$submissionManager = $this->createMock(SubmissionManager::class);
52+
$submissionManager->method('find')->willReturnCallback(function (array $arguments) use ($submissions) {
53+
if (array_key_exists(SubmissionEntity::FIELD_TARGET_BLOG_ID, $arguments)) {
54+
return array_filter($submissions, static function (SubmissionEntity $submission) use ($arguments) {
55+
return $submission->getTargetBlogId() === $arguments[SubmissionEntity::FIELD_TARGET_BLOG_ID];
56+
});
57+
}
58+
59+
return [];
60+
});
61+
$submissionManager->expects($this->exactly(2))->method('delete');
62+
63+
$apiWrapper = $this->createMock(ApiWrapperInterface::class);
64+
$matcher = $this->exactly(2);
65+
$apiWrapper->expects($matcher)
66+
->method('createAuditLogRecord')
67+
->willReturnCallback(function (ConfigurationProfileEntity $profile, string $actionType, string $description) use ($matcher, $profiles) {
68+
$this->assertEquals($profiles[$matcher->getInvocationCount() - 1], $profile);
69+
$this->assertEquals(CreateRecordParameters::ACTION_TYPE_DELETE, $actionType);
70+
switch ($matcher->getInvocationCount()) {
71+
case 1:
72+
$this->assertEquals('Blog deletion handler, submissionId=12, fileUri=12', $description);
73+
break;
74+
case 2:
75+
$this->assertEquals('Blog deletion handler, submissionId=32, fileUri=32', $description);
76+
break;
77+
}
78+
});
79+
80+
(new BlogRemovalHandler($apiWrapper, $settingsManager, $submissionManager))->blogRemovalHandler(2);
81+
}
82+
}

0 commit comments

Comments
 (0)