summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Unit/Maintenance/DuplicateEntitiesDisposerTest.php
blob: b1f6cb107da8e24d91e88ae2c907207d1054e383 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php

namespace SMW\Tests\Maintenance;

use FakeResultWrapper;
use SMW\Maintenance\DuplicateEntitiesDisposer;

/**
 * @covers \SMW\Maintenance\DuplicateEntitiesDisposer
 * @group semantic-mediawiki
 *
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class DuplicateEntitiesDisposerTest extends \PHPUnit_Framework_TestCase {

	private $store;
	private $cache;
	private $messageReporter;
	private $connection;
	private $propertyTableIdReferenceFinder;

	protected function setUp() {

		$this->store = $this->getMockBuilder( '\SMW\SQLStore\SQLStore' )
			->disableOriginalConstructor()
			->getMock();

		$this->propertyTableIdReferenceFinder = $this->getMockBuilder( '\SMW\SQLStore\PropertyTableIdReferenceFinder' )
			->disableOriginalConstructor()
			->getMock();

		$this->store->expects( $this->any() )
			->method( 'getPropertyTableIdReferenceFinder' )
			->will( $this->returnValue( $this->propertyTableIdReferenceFinder ) );

		$this->messageReporter = $this->getMockBuilder( '\Onoi\MessageReporter\MessageReporter' )
			->disableOriginalConstructor()
			->getMock();

		$this->connection = $this->getMockBuilder( '\SMW\MediaWiki\Database' )
			->disableOriginalConstructor()
			->getMock();

		$this->cache = $this->getMockBuilder( '\Onoi\Cache\Cache' )
			->disableOriginalConstructor()
			->getMock();
	}

	public function testCanConstruct() {

		$this->assertInstanceOf(
			DuplicateEntitiesDisposer::class,
			new DuplicateEntitiesDisposer( $this->store )
		);
	}

	public function testFindDuplicateEntityRecords() {

		$idTable = $this->getMockBuilder( '\stdClss' )
			->disableOriginalConstructor()
			->setMethods( [ 'findDuplicates' ] )
			->getMock();

		$this->store->expects( $this->atLeastOnce() )
			->method( 'getObjectIds' )
			->will( $this->returnValue( $idTable ) );

		$instance = new DuplicateEntitiesDisposer(
			$this->store
		);

		$instance->findDuplicates();
	}

	public function testVerifyAndDispose_NoDuplicates() {

		$this->store->expects( $this->never() )
			->method( 'getConnection' );

		$instance = new DuplicateEntitiesDisposer(
			$this->store
		);

		$instance->setMessageReporter(
			$this->messageReporter
		);

		$instance->verifyAndDispose( [] );
	}

	public function testVerifyAndDispose_NonIterable() {

		$this->store->expects( $this->never() )
			->method( 'getConnection' );

		$instance = new DuplicateEntitiesDisposer(
			$this->store
		);

		$instance->setMessageReporter(
			$this->messageReporter
		);

		$instance->verifyAndDispose( 'Foo' );
	}

	public function testVerifyAndDispose_NoDuplicates_WithCache() {

		$this->store->expects( $this->never() )
			->method( 'getConnection' );

		$this->cache->expects( $this->atLeastOnce() )
			->method( 'delete' );

		$instance = new DuplicateEntitiesDisposer(
			$this->store,
			$this->cache
		);

		$instance->setMessageReporter(
			$this->messageReporter
		);

		$instance->verifyAndDispose( [] );
	}

	public function testVerifyAndDispose_WithDuplicateRecord() {

		$record = [
			'smw_title' => 'Foo',
			'smw_namespace' => 0,
			'smw_iw' => '',
			'smw_subobject' => ''
		];

		$row = new \stdClass;
		$row->smw_id = 42;

		$this->connection->expects( $this->atLeastOnce() )
			->method( 'select' )
			->with(
				$this->anything(),
				$this->anything(),
				$this->equalTo( $record ),
				$this->anything() )
			->will( $this->returnValue( [ $row ] ) );

		$this->store->expects( $this->atLeastOnce() )
			->method( 'getConnection' )
			->will( $this->returnValue( $this->connection ) );

		$idTable = $this->getMockBuilder( '\stdClss' )
			->disableOriginalConstructor()
			->setMethods( [ 'getDataItemById' ] )
			->getMock();

		$this->store->expects( $this->atLeastOnce() )
			->method( 'getObjectIds' )
			->will( $this->returnValue( $idTable ) );

		$this->store->expects( $this->any() )
			->method( 'getPropertyTables' )
			->will( $this->returnValue( [] ) );

		$this->propertyTableIdReferenceFinder->expects( $this->atLeastOnce() )
			->method( 'hasResidualReferenceForId' )
			->will( $this->returnValue( false ) );

		$instance = new DuplicateEntitiesDisposer(
			$this->store
		);

		$instance->setMessageReporter(
			$this->messageReporter
		);

		$duplicates = [
			$record
		];

		$instance->verifyAndDispose( $duplicates );
	}

}