View source
<?php
namespace Drupal\Tests\node_revision_delete\Unit;
use Drupal\node_revision_delete\NodeRevisionDeleteBatch;
use Drupal\Tests\UnitTestCase;
use Drupal\node_revision_delete\NodeRevisionDelete;
use Drupal\Tests\node_revision_delete\Traits\NodeRevisionDeleteTestTrait;
class NodeRevisionDeleteTest extends UnitTestCase {
use NodeRevisionDeleteTestTrait;
protected $connection;
protected $configFactory;
protected $entityTypeManager;
protected $languageManager;
protected $nodeRevisionDelete;
protected $configFile;
public function setUp() {
parent::setUp();
$this->configFile = 'node_revision_delete.settings';
$this->connection = $this
->createMock('Drupal\\Core\\Database\\Connection');
$this->configFactory = $this
->createMock('Drupal\\Core\\Config\\ConfigFactoryInterface');
$this->entityTypeManager = $this
->createMock('Drupal\\Core\\Entity\\EntityTypeManagerInterface');
$this->languageManager = $this
->createMock('Drupal\\Core\\Language\\LanguageManagerInterface');
$this->nodeRevisionDelete = new NodeRevisionDelete($this->configFactory, $this
->getStringTranslationStub(), $this->connection, $this->entityTypeManager, $this->languageManager);
}
public function testGetTimeString($expected, array $config_name_time, $config_name, $number) {
$config = $this
->createMock('Drupal\\Core\\Config\\ImmutableConfig');
$config
->expects($this
->any())
->method('get')
->with('node_revision_delete_' . $config_name . '_time')
->willReturn($config_name_time);
$this->configFactory
->expects($this
->any())
->method('get')
->with($this->configFile)
->willReturn($config);
$this
->assertEquals($expected, $this->nodeRevisionDelete
->getTimeString($config_name, $number));
}
public function providerGetTimeString() {
$expected = [
'5 days',
'2 days',
'1 day',
'10 weeks',
'20 weeks',
'1 week',
'12 months',
'24 months',
'1 month',
$this
->getStringTranslationStub()
->translate('After @number @time of inactivity', [
'@number' => 5,
'@time' => 'days',
]),
$this
->getStringTranslationStub()
->translate('After @number @time of inactivity', [
'@number' => 2,
'@time' => 'days',
]),
$this
->getStringTranslationStub()
->translate('After @number @time of inactivity', [
'@number' => 1,
'@time' => 'day',
]),
$this
->getStringTranslationStub()
->translate('After @number @time of inactivity', [
'@number' => 10,
'@time' => 'weeks',
]),
$this
->getStringTranslationStub()
->translate('After @number @time of inactivity', [
'@number' => 20,
'@time' => 'weeks',
]),
$this
->getStringTranslationStub()
->translate('After @number @time of inactivity', [
'@number' => 1,
'@time' => 'week',
]),
$this
->getStringTranslationStub()
->translate('After @number @time of inactivity', [
'@number' => 12,
'@time' => 'months',
]),
$this
->getStringTranslationStub()
->translate('After @number @time of inactivity', [
'@number' => 24,
'@time' => 'months',
]),
$this
->getStringTranslationStub()
->translate('After @number @time of inactivity', [
'@number' => 1,
'@time' => 'month',
]),
];
$days = [
'time' => 'days',
];
$weeks = [
'time' => 'weeks',
];
$months = [
'time' => 'months',
];
$tests['days 1'] = [
$expected[0],
$days,
'minimum_age_to_delete',
5,
];
$tests['days 2'] = [
$expected[1],
$days,
'minimum_age_to_delete',
2,
];
$tests['days 3'] = [
$expected[2],
$days,
'minimum_age_to_delete',
1,
];
$tests['weeks 1'] = [
$expected[3],
$weeks,
'minimum_age_to_delete',
10,
];
$tests['weeks 2'] = [
$expected[4],
$weeks,
'minimum_age_to_delete',
20,
];
$tests['weeks 3'] = [
$expected[5],
$weeks,
'minimum_age_to_delete',
1,
];
$tests['months 1'] = [
$expected[6],
$months,
'minimum_age_to_delete',
12,
];
$tests['months 2'] = [
$expected[7],
$months,
'minimum_age_to_delete',
24,
];
$tests['months 3'] = [
$expected[8],
$months,
'minimum_age_to_delete',
1,
];
$tests['days 4'] = [
$expected[9],
$days,
'when_to_delete',
5,
];
$tests['days 5'] = [
$expected[10],
$days,
'when_to_delete',
2,
];
$tests['days 6'] = [
$expected[11],
$days,
'when_to_delete',
1,
];
$tests['weeks 4'] = [
$expected[12],
$weeks,
'when_to_delete',
10,
];
$tests['weeks 5'] = [
$expected[13],
$weeks,
'when_to_delete',
20,
];
$tests['weeks 6'] = [
$expected[14],
$weeks,
'when_to_delete',
1,
];
$tests['months 4'] = [
$expected[15],
$months,
'when_to_delete',
12,
];
$tests['months 5'] = [
$expected[16],
$months,
'when_to_delete',
24,
];
$tests['months 6'] = [
$expected[17],
$months,
'when_to_delete',
1,
];
return $tests;
}
public function testGetTimeValues($expected, $index) {
$this
->assertEquals($expected, $this->nodeRevisionDelete
->getTimeValues($index));
}
public function providerGetTimeValues() {
$all_values = [
'-1' => 'Never',
'0' => 'Every time cron runs',
'3600' => 'Every hour',
'86400' => 'Everyday',
'604800' => 'Every week',
'864000' => 'Every 10 days',
'1296000' => 'Every 15 days',
'2592000' => 'Every month',
'7776000' => 'Every 3 months',
'15552000' => 'Every 6 months',
'31536000' => 'Every year',
'63072000' => 'Every 2 years',
];
$tests[] = [
$all_values,
NULL,
];
$tests[] = [
$all_values[-1],
-1,
];
$tests[] = [
$all_values[0],
0,
];
$tests[] = [
$all_values[3600],
3600,
];
$tests[] = [
$all_values[86400],
86400,
];
$tests[] = [
$all_values[604800],
604800,
];
$tests[] = [
$all_values[864000],
864000,
];
$tests[] = [
$all_values[1296000],
1296000,
];
$tests[] = [
$all_values[2592000],
2592000,
];
$tests[] = [
$all_values[7776000],
7776000,
];
$tests[] = [
$all_values[15552000],
15552000,
];
$tests[] = [
$all_values[31536000],
31536000,
];
$tests[] = [
$all_values[63072000],
63072000,
];
return $tests;
}
public function testGetTimeNumberString($expected, $number, $time) {
$this
->assertEquals($expected, $this->nodeRevisionDelete
->getTimeNumberString($number, $time));
}
public function providerGetTimeNumberString() {
$tests['day singular'] = [
'day',
1,
'days',
];
$tests['day plural 1'] = [
'days',
2,
'days',
];
$tests['day plural 2'] = [
'days',
10,
'days',
];
$tests['week singular'] = [
'week',
1,
'weeks',
];
$tests['week plural 1'] = [
'weeks',
2,
'weeks',
];
$tests['week plural 2'] = [
'weeks',
10,
'weeks',
];
$tests['month singular'] = [
'month',
1,
'months',
];
$tests['month plural 1'] = [
'months',
2,
'months',
];
$tests['month plural 2'] = [
'months',
10,
'months',
];
return $tests;
}
public function testGetRevisionDeletionBatch(array $expected, array $revisions, $dry_run) {
$this
->assertEquals($expected, $this->nodeRevisionDelete
->getRevisionDeletionBatch($revisions, $dry_run));
}
public function providerGetRevisionDeletionBatch() {
$revision_sets = [
[],
[
12,
],
[
32,
4,
],
[
45,
23,
72,
],
[
76,
97,
34,
53,
],
];
$dry_run_set = [
TRUE,
TRUE,
TRUE,
FALSE,
FALSE,
];
$batch_template = [
'title' => $this
->getStringTranslationStub()
->translate('Deleting revisions'),
'init_message' => $this
->getStringTranslationStub()
->translate('Starting to delete revisions.'),
'progress_message' => $this
->getStringTranslationStub()
->translate('Deleted @current out of @total (@percentage%). Estimated time: @estimate.'),
'error_message' => $this
->getStringTranslationStub()
->translate('Error deleting revisions.'),
'operations' => [],
'finished' => [
NodeRevisionDeleteBatch::class,
'finish',
],
'file' => NULL,
'library' => [],
'url_options' => [],
'progressive' => TRUE,
];
$expected = [];
foreach ($revision_sets as $set => $revisions) {
$expected[$set] = $batch_template;
foreach ($revisions as $revision) {
$expected[$set]['operations'][] = [
[
NodeRevisionDeleteBatch::class,
'deleteRevision',
],
[
$revision,
$dry_run_set[$set],
],
];
}
}
$tests[] = [
$expected[0],
$revision_sets[0],
$dry_run_set[0],
];
$tests[] = [
$expected[1],
$revision_sets[1],
$dry_run_set[1],
];
$tests[] = [
$expected[2],
$revision_sets[2],
$dry_run_set[2],
];
$tests[] = [
$expected[3],
$revision_sets[3],
$dry_run_set[3],
];
$tests[] = [
$expected[4],
$revision_sets[4],
$dry_run_set[4],
];
return $tests;
}
public function testGetRelativeTime($expected, array $time, $config_name, $number) {
$config = $this
->createMock('Drupal\\Core\\Config\\ImmutableConfig');
$config
->expects($this
->any())
->method('get')
->with('node_revision_delete_' . $config_name . '_time')
->willReturn($time);
$this->configFactory
->expects($this
->any())
->method('get')
->with($this->configFile)
->willReturn($config);
$this
->assertEquals($expected, $this->nodeRevisionDelete
->getRelativeTime($config_name, $number));
}
public function providerGetRelativeTime() {
$expected = [
strtotime('-5 days'),
strtotime('-2 days'),
strtotime('-1 day'),
strtotime('-10 weeks'),
strtotime('-20 weeks'),
strtotime('-1 week'),
strtotime('-12 months'),
strtotime('-24 months'),
strtotime('-1 month'),
];
$days = [
'time' => 'days',
];
$weeks = [
'time' => 'weeks',
];
$months = [
'time' => 'months',
];
$tests['days 1'] = [
$expected[0],
$days,
'minimum_age_to_delete',
5,
];
$tests['days 2'] = [
$expected[1],
$days,
'minimum_age_to_delete',
2,
];
$tests['days 3'] = [
$expected[2],
$days,
'minimum_age_to_delete',
1,
];
$tests['weeks 1'] = [
$expected[3],
$weeks,
'minimum_age_to_delete',
10,
];
$tests['weeks 2'] = [
$expected[4],
$weeks,
'minimum_age_to_delete',
20,
];
$tests['weeks 3'] = [
$expected[5],
$weeks,
'minimum_age_to_delete',
1,
];
$tests['months 1'] = [
$expected[6],
$months,
'minimum_age_to_delete',
12,
];
$tests['months 2'] = [
$expected[7],
$months,
'minimum_age_to_delete',
24,
];
$tests['months 3'] = [
$expected[8],
$months,
'minimum_age_to_delete',
1,
];
$tests['days 4'] = [
$expected[0],
$days,
'when_to_delete',
5,
];
$tests['days 5'] = [
$expected[1],
$days,
'when_to_delete',
2,
];
$tests['days 6'] = [
$expected[2],
$days,
'when_to_delete',
1,
];
$tests['weeks 4'] = [
$expected[3],
$weeks,
'when_to_delete',
10,
];
$tests['weeks 5'] = [
$expected[4],
$weeks,
'when_to_delete',
20,
];
$tests['weeks 6'] = [
$expected[5],
$weeks,
'when_to_delete',
1,
];
$tests['months 4'] = [
$expected[6],
$months,
'when_to_delete',
12,
];
$tests['months 5'] = [
$expected[7],
$months,
'when_to_delete',
24,
];
$tests['months 6'] = [
$expected[8],
$months,
'when_to_delete',
1,
];
return $tests;
}
public function testGetConfiguredContentTypes(array $expected, array $third_party_settings, array $content_types_list) {
$config = $this
->createMock('Drupal\\Core\\Config\\ImmutableConfig');
$config
->expects($this
->any())
->method('get')
->with('third_party_settings')
->willReturnOnConsecutiveCalls(...$third_party_settings);
$cant = count($content_types_list);
$map_node_type = [];
for ($i = 0; $i < $cant; $i++) {
$map_node_type[] = [
'node.type.' . $content_types_list[$i]
->id(),
$config,
];
}
$this->configFactory
->expects($this
->any())
->method('get')
->will($this
->returnValueMap($map_node_type));
$entity_storage = $this
->createMock('Drupal\\Core\\Entity\\EntityStorageInterface');
$entity_storage
->expects($this
->any())
->method('loadMultiple')
->willReturn($content_types_list);
$this->entityTypeManager
->expects($this
->any())
->method('getStorage')
->with('node_type')
->willReturn($entity_storage);
$this
->assertEquals($expected, $this->nodeRevisionDelete
->getConfiguredContentTypes());
}
public function providerGetConfiguredContentTypes() {
$third_party_settings = [
[
[
'node_revision_delete' => [
0,
],
],
[
'www' => [
1,
],
],
[
'node_revision_delete' => [
2,
],
],
],
[
[
'xxx' => [
0,
],
],
[
'node_revision_delete' => [
1,
],
],
[
'yyy' => [
2,
],
],
[
'node_revision_delete' => [
3,
],
],
],
[
[
'xxx' => [
1,
],
],
[
'yyy' => [
3,
],
],
],
[
[
'node_revision_delete' => [
1,
],
],
[
'node_revision_delete' => [
2,
],
],
[
'node_revision_delete' => [
3,
],
],
],
[
[
'node_revision_delete' => [
1,
],
],
],
];
$content_types_list = $this
->getConfiguredContentTypes();
$expected = [
[
$content_types_list[0]['0'],
$content_types_list[0]['2'],
],
[
$content_types_list[1]['1'],
$content_types_list[1]['3'],
],
[],
[
$content_types_list[3]['0'],
$content_types_list[3]['1'],
$content_types_list[3]['2'],
],
[
$content_types_list[4]['0'],
],
];
$tests = [];
$tests[] = [
$expected[0],
$third_party_settings[0],
$content_types_list[0],
];
$tests[] = [
$expected[1],
$third_party_settings[1],
$content_types_list[1],
];
$tests[] = [
$expected[2],
$third_party_settings[2],
$content_types_list[2],
];
$tests[] = [
$expected[3],
$third_party_settings[3],
$content_types_list[3],
];
$tests[] = [
$expected[4],
$third_party_settings[4],
$content_types_list[4],
];
return $tests;
}
private function getConfiguredContentTypes() {
$content_types = [
[
'article',
'page',
'test',
],
[
'article',
'blog',
'house',
'page',
],
[
'blog',
'house',
],
[
'article',
'blog',
'page',
],
[
'blog',
],
];
$number_of_tests = count($content_types);
$content_types_list = [];
for ($i = 0; $i < $number_of_tests; $i++) {
$j = 0;
foreach ($content_types[$i] as $id) {
$content_types_list[$i][$j] = $this
->createMock('Drupal\\Core\\Entity\\EntityInterface');
$content_types_list[$i][$j]
->expects($this
->any())
->method('id')
->willReturn($id);
$j++;
}
}
return $content_types_list;
}
private function getNodeRevisionDeleteMock(array $content_types_list) {
$controller = $this
->getMockBuilder('Drupal\\node_revision_delete\\NodeRevisionDelete')
->setConstructorArgs([
$this->configFactory,
$this
->getStringTranslationStub(),
$this->connection,
$this->entityTypeManager,
$this->languageManager,
])
->setMethods([
'getConfiguredContentTypes',
])
->getMock();
$controller
->expects($this
->any())
->method('getConfiguredContentTypes')
->willReturn($content_types_list);
return $controller;
}
public function testUpdateTimeMaxNumberConfig(array $third_party_settings, array $content_types_list, $max_number) {
$controller = $this
->getNodeRevisionDeleteMock($content_types_list);
$config = $this
->createMock('Drupal\\Core\\Config\\Config');
$config
->expects($this
->any())
->method('get')
->with('third_party_settings')
->willReturnOnConsecutiveCalls(...$third_party_settings);
$config
->expects($this
->any())
->method('set')
->with('third_party_settings', $this
->anything())
->willReturnSelf();
$config
->expects($this
->any())
->method('save');
$cant = count($content_types_list);
$map_node_type = [];
for ($i = 0; $i < $cant; $i++) {
$map_node_type[] = [
'node.type.' . $content_types_list[$i]
->id(),
$config,
];
}
$this->configFactory
->expects($this
->any())
->method('getEditable')
->will($this
->returnValueMap($map_node_type));
$this
->assertNull($controller
->updateTimeMaxNumberConfig('minimum_age_to_delete', $max_number));
$this
->assertNull($controller
->updateTimeMaxNumberConfig('when_to_delete', $max_number));
}
public function providerUpdateTimeMaxNumberConfig() {
$third_party_settings = [
[
[
'node_revision_delete' => [
'minimum_age_to_delete' => 5,
'when_to_delete' => 3,
],
],
[
'node_revision_delete' => [
'minimum_age_to_delete' => 10,
'when_to_delete' => 12,
],
],
[
'node_revision_delete' => [
'minimum_age_to_delete' => 5,
'when_to_delete' => 5,
],
],
],
[
[
'node_revision_delete' => [
'minimum_age_to_delete' => 5,
'when_to_delete' => 3,
],
],
[
'node_revision_delete' => [
'minimum_age_to_delete' => 10,
'when_to_delete' => 12,
],
],
[
'node_revision_delete' => [
'minimum_age_to_delete' => 8,
'when_to_delete' => 2,
],
],
[
'node_revision_delete' => [
'minimum_age_to_delete' => 3,
'when_to_delete' => 2,
],
],
],
[
[
'node_revision_delete' => [
'minimum_age_to_delete' => 10,
'when_to_delete' => 12,
],
],
[
'node_revision_delete' => [
'minimum_age_to_delete' => 8,
'when_to_delete' => 2,
],
],
],
[
[
'node_revision_delete' => [
'minimum_age_to_delete' => 15,
'when_to_delete' => 13,
],
],
[
'node_revision_delete' => [
'minimum_age_to_delete' => 1,
'when_to_delete' => 1,
],
],
[
'node_revision_delete' => [
'minimum_age_to_delete' => 15,
'when_to_delete' => 15,
],
],
],
[
[
'node_revision_delete' => [
'minimum_age_to_delete' => 5,
'when_to_delete' => 3,
],
],
],
];
$content_types_list = $this
->getConfiguredContentTypes();
$tests = [];
$tests[] = [
$third_party_settings[0],
$content_types_list[0],
2,
];
$tests[] = [
$third_party_settings[1],
$content_types_list[1],
4,
];
$tests[] = [
$third_party_settings[2],
$content_types_list[2],
8,
];
$tests[] = [
$third_party_settings[3],
$content_types_list[3],
14,
];
$tests[] = [
$third_party_settings[4],
$content_types_list[4],
2,
];
return $tests;
}
public function testGetContentTypeConfig($expected, array $third_party_settings, $content_type) {
$config = $this
->createMock('Drupal\\Core\\Config\\ImmutableConfig');
$config
->expects($this
->any())
->method('get')
->with('third_party_settings')
->willReturn($third_party_settings);
$this->configFactory
->expects($this
->any())
->method('get')
->with('node.type.' . $content_type)
->willReturn($config);
$this
->assertEquals($expected, $this->nodeRevisionDelete
->getContentTypeConfig($content_type));
}
public function providerGetContentTypeConfig() {
$content_types = [
'article',
'page',
'test',
'car',
'house',
];
$third_party_settings = [
[
'node_revision_delete' => [
0,
],
],
[
'xxx' => [
0,
],
],
[
'xxx' => [
1,
],
],
[
'node_revision_delete' => [
1,
],
],
[
'node_revision_delete' => [
2,
],
],
];
$expected = [
[
0,
],
[],
[],
[
1,
],
[
2,
],
];
$tests = [];
$tests[] = [
$expected[0],
$third_party_settings[0],
$content_types[0],
];
$tests[] = [
$expected[1],
$third_party_settings[1],
$content_types[1],
];
$tests[] = [
$expected[2],
$third_party_settings[2],
$content_types[2],
];
$tests[] = [
$expected[3],
$third_party_settings[3],
$content_types[3],
];
$tests[] = [
$expected[4],
$third_party_settings[4],
$content_types[4],
];
return $tests;
}
}