8 namespace Fossology\Reuser;
11 $autoloadPath = __DIR__ .
'/../../../../vendor/autoload.php';
12 if (file_exists($autoloadPath)) {
13 require_once $autoloadPath;
15 require_once __DIR__ .
'/../../agent/ReuserAgent.php';
30 private $copyrightDao;
32 protected function setUp():
void
34 $this->
dbManager = $this->getMockBuilder(DbManager::class)->disableOriginalConstructor()->getMock();
35 $this->uploadDao = $this->getMockBuilder(UploadDao::class)->disableOriginalConstructor()->getMock();
36 $this->copyrightDao = $this->getMockBuilder(CopyrightDao::class)->disableOriginalConstructor()->getMock();
48 $tableName =
"uploadtree";
50 $this->uploadDao->method(
'getUploadtreeTableName')->willReturn($tableName);
53 'c1' => [
'hash' =>
'hash1',
'uploadtree_pk' => 101,
'content' =>
'© Meta'],
54 'c2' => [
'hash' =>
'hash2',
'uploadtree_pk' => 102,
'content' =>
'© Google'],
58 [
'hash' =>
'hash1',
'is_enabled' =>
't',
'contentedited' =>
'© Meta Edited'],
59 [
'hash' =>
'hash2',
'is_enabled' =>
'f',
'contentedited' =>
''],
62 $this->copyrightDao->method(
'getScannerEntries')->willReturn($allCopyrights);
63 $this->copyrightDao->method(
'getAllEventEntriesForUpload')->willReturn($reusedCopyrights);
64 $this->
dbManager->method(
'booleanFromDb')->willReturnCallback(
function($val) {
68 $item1 = $this->createMock(ItemTreeBounds::class);
69 $item2 = $this->createMock(ItemTreeBounds::class);
70 $this->uploadDao->method(
'getItemTreeBounds')->willReturnMap([
71 [101, $tableName, $item1],
72 [102, $tableName, $item2],
75 $this->copyrightDao->expects($this->exactly(2))->method(
'updateTable');
77 $mockReuserAgent = $this->getMockBuilder(ReuserAgent::class)
78 ->disableOriginalConstructor()
79 ->onlyMethods([
'getAgentId',
'heartbeat'])
82 $reflection = new \ReflectionClass(ReuserAgent::class);
83 foreach ([
'uploadDao',
'copyrightDao',
'dbManager',
'userId'] as $propName) {
84 $prop = $reflection->getProperty($propName);
85 $prop->setAccessible(
true);
86 if ($propName ===
'userId') {
87 $prop->setValue($mockReuserAgent, 1);
89 $prop->setValue($mockReuserAgent, $this->$propName);
93 $mockReuserAgent->method(
'getAgentId')->willReturn($agentId);
94 $mockReuserAgent->method(
'heartbeat')->willReturn(
null);
96 $method = $reflection->getMethod(
'reuseCopyrights');
97 $method->setAccessible(
true);
98 $method->invoke($mockReuserAgent, $uploadId, $reusedUploadId);
100 $this->assertTrue(
true);
111 $tableName =
"uploadtree";
114 'c1' => [
'hash' =>
'shared',
'uploadtree_pk' => 101],
115 'c2' => [
'hash' =>
'shared',
'uploadtree_pk' => 102],
118 $reusedCopyrights = [
119 [
'hash' =>
'shared',
'is_enabled' =>
't',
'contentedited' =>
'Edit 1'],
120 [
'hash' =>
'shared',
'is_enabled' =>
't',
'contentedited' =>
'Edit 2'],
123 $this->uploadDao->method(
'getUploadtreeTableName')->willReturn($tableName);
124 $this->copyrightDao->method(
'getScannerEntries')->willReturn($allCopyrights);
125 $this->copyrightDao->method(
'getAllEventEntriesForUpload')->willReturn($reusedCopyrights);
126 $this->
dbManager->method(
'booleanFromDb')->willReturn(
true);
128 $this->uploadDao->method(
'getItemTreeBounds')->willReturn($this->createMock(ItemTreeBounds::class));
130 $mockReuserAgent = $this->getMockBuilder(ReuserAgent::class)
131 ->disableOriginalConstructor()
132 ->onlyMethods([
'getAgentId',
'heartbeat'])
135 $reflection = new \ReflectionClass(ReuserAgent::class);
136 foreach ([
'uploadDao',
'copyrightDao',
'dbManager',
'userId'] as $propName) {
137 $prop = $reflection->getProperty($propName);
138 $prop->setAccessible(
true);
139 if ($propName ===
'userId') {
140 $prop->setValue($mockReuserAgent, 1);
142 $prop->setValue($mockReuserAgent, $this->$propName);
146 $mockReuserAgent->method(
'getAgentId')->willReturn(10);
147 $mockReuserAgent->method(
'heartbeat')->willReturn(
null);
149 $this->copyrightDao->expects($this->exactly(2))->method(
'updateTable');
151 $method = $reflection->getMethod(
'reuseCopyrights');
152 $method->setAccessible(
true);
153 $method->invoke($mockReuserAgent, $uploadId, $reusedUploadId);
155 $this->assertTrue(
true);
Unit test for ReuserAgent matching logic.
testReuseCopyrightsWithDuplicateHashes()
fo_dbManager * dbManager
fo_dbManager object