FOSSology  4.7.1
Open Source License Compliance by Open Source Software
ClearingDaoTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
4  Author: Andreas Würl, Johannes Najjar
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 namespace Fossology\Lib\Dao;
10 
19 use Monolog\Logger;
20 use Monolog\Handler\ErrorLogHandler;
21 use Mockery as M;
22 use Mockery\MockInterface;
23 
24 class ClearingDaoTest extends \PHPUnit\Framework\TestCase
25 {
26  public $assertCountBefore;
27 
29  private $testDb;
31  private $dbManager;
33  private $uploadDao;
35  private $clearingDao;
37  private $now;
39  private $items;
40  private $groupId = 601;
41 
42 
43  protected function setUp() : void
44  {
45  $this->uploadDao = M::mock(UploadDao::class);
46  $this->uploadDao->shouldReceive('getUploadEntry')->withAnyArgs()->andReturn(["upload_fk" => 4]);
47 
48  $logger = new Logger('default');
49  $logger->pushHandler(new ErrorLogHandler());
50 
51  $this->testDb = new TestPgDb();
52  $this->dbManager = &$this->testDb->getDbManager();
53 
54  $this->clearingDao = new ClearingDao($this->dbManager, $this->uploadDao);
55 
56  $this->testDb->createPlainTables(
57  array(
58  'clearing_decision',
59  'clearing_decision_event',
60  'clearing_decision_type',
61  'clearing_event',
62  'clearing_licenses',
63  'custom_phrase',
64  'custom_phrase_license_map',
65  'highlight_bulk',
66  'highlight_kotoba',
67  'license_ref',
68  'license_ref_bulk',
69  'license_set_bulk',
70  'users',
71  'uploadtree'
72  ));
73 
74  $this->testDb->createInheritedTables();
75 
76  $userArray = array(
77  array('myself', 1),
78  array('in_same_group', 2),
79  array('in_trusted_group', 3));
80  foreach ($userArray as $ur) {
81  $this->dbManager->insertInto('users', 'user_name, root_folder_fk', $ur);
82  }
83 
84  $refArray = array(
85  array(401, 'FOO', 'FOO', 'foo full', 'foo text'),
86  array(402, 'BAR', 'BAR', 'bar full', 'bar text'),
87  array(403, 'BAZ', 'BAZ', 'baz full', 'baz text'),
88  array(404, 'QUX', 'QUX', 'qux full', 'qux text')
89  );
90  foreach ($refArray as $params) {
91  $this->dbManager->insertInto('license_ref', 'rf_pk, rf_shortname, rf_spdx_id, rf_fullname, rf_text', $params, $logStmt = 'insert.ref');
92  }
93 
94  $modd = 536888320;
95  $modf = 33188;
96 
97  /* (pfile,item,lft,rgt)
98  upload101: upload101/ ( 0, 299, 1, 4)
99  Afile (201, 301, 1, 2)
100  Bfile (202, 302, 3, 4)
101  upload102: upload102/ ( 0, 300, 1, 8)
102  Afile (201, 303, 1, 2)
103  A-dir/ ( 0, 304, 3, 6)
104  A-dir/Afile (201, 305, 4, 5)
105  Bfile (202, 306, 7, 8)
106  */
107  $this->items = array(
108  299=>array(101, 299, 0, $modd, 1, 4, "upload101"),
109  300=>array(102, 300, 0, $modd, 1, 8, "upload102"),
110  301=>array(101, 301, 201, $modf, 1, 2, "Afile"),
111  302=>array(101, 302, 202, $modf, 3, 4, "Bfile"),
112  303=>array(102, 303, 201, $modf, 1, 2, "Afile"),
113  304=>array(102, 304, 0, $modd, 3, 6, "A-dir"),
114  305=>array(102, 305, 201, $modf, 4, 5, "Afile"),
115  306=>array(102, 306, 202, $modf, 7, 8, "Bfile"),
116  );
117  foreach ($this->items as $ur) {
118  $this->dbManager->insertInto('uploadtree', 'upload_fk,uploadtree_pk,pfile_fk,ufile_mode,lft,rgt,ufile_name', $ur);
119  }
120  $this->now = time();
121 
122  $bulkLicArray = array(
123  array(1, 401, 'TextFOO', false, 101, 299, $this->groupId),
124  array(2, 402, 'TextBAR', false, 101, 299, $this->groupId),
125  array(3, 403, 'TextBAZ', true, 101, 301, $this->groupId),
126  array(4, 403, 'TextBAZ', false, 101, 299, $this->groupId),
127  array(5, 404, 'TextQUX', true, 101, 299, $this->groupId),
128  array(6, 401, 'TexxFOO', true, 101, 302, $this->groupId),
129  array(7, 403, 'TextBAZ', false, 102, 300, $this->groupId),
130  array(8, 403, 'TextBAZ', true, 102, 306, $this->groupId)
131  );
132  foreach ($bulkLicArray as $params) {
133  $paramsRef = array($params[0], $params[2], $params[4], $params[5], $params[6]);
134  $paramsSet = array($params[0], $params[1], $params[3]);
135  $this->dbManager->insertInto('license_ref_bulk', 'lrb_pk, rf_text, upload_fk, uploadtree_fk, group_fk', $paramsRef, 'insert.bulkref');
136  $this->dbManager->insertInto('license_set_bulk', 'lrb_fk, rf_fk, removing', $paramsSet, 'insert.bulkset');
137  }
138 
139  // cp_pk => [text, [[rf_fk, removing], ...]]. Phrase 2 carries both an
140  // add and a remove mapping, to catch the empty-reportinfo case.
141  $kotobaPhrases = array(
142  1 => array('TextFOO', array(array(401, false))),
143  2 => array('TextMixed', array(array(401, false), array(402, true))),
144  );
145  foreach ($kotobaPhrases as $cpPk => $phrase) {
146  list($text, $mappings) = $phrase;
147  $this->dbManager->insertInto('custom_phrase', 'cp_pk, group_fk, text, text_md5, is_active',
148  array($cpPk, $this->groupId, $text, md5('phrase' . $cpPk), true), 'insert.customphrase');
149  foreach ($mappings as $mapping) {
150  list($rfFk, $removing) = $mapping;
151  $this->dbManager->insertInto('custom_phrase_license_map', 'cp_fk, rf_fk, removing',
152  array($cpPk, $rfFk, $removing), 'insert.customphraselicensemap');
153  }
154  }
155 
156  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
157  }
158 
159  private function insertBulkEvents()
160  {
161  $bulkFindingsArray = array(
162  array(1, 5001),
163  array(1, 5001),// a second bulk match in the same file in a different place
164  array(1, 5002),
165  array(1, 5003),
166  array(4, 5004),
167  array(7, 5005)
168  );
169  foreach ($bulkFindingsArray as $params) {
170  $this->dbManager->insertInto('highlight_bulk', 'lrb_fk, clearing_event_fk', $params, $logStmt = 'insert.bulkfinds');
171  }
172 
173  $bulkClearingEvents = array(
174  array(5001, 301),
175  array(5002, 302),
176  array(5003, 303),
177  array(5004, 301),
178  array(5005, 305)
179  );
180  foreach ($bulkClearingEvents as $params) {
181  $this->dbManager->insertInto('clearing_event', 'clearing_event_pk, uploadtree_fk', $params, $logStmt = 'insert.bulkevents');
182  }
183  }
184 
190  private function insertKotobaEvents()
191  {
192  $kotobaClearingEvents = array(
193  // clearing_event_pk, uploadtree_fk, cp_fk, has a highlight_kotoba row
194  array(6001, 302, 1, true), // phrase 1 (FOO, add), matched on 302 only
195  array(6002, 301, 2, true), // phrase 2 (Mixed), add mapping, anchor
196  array(6003, 301, 2, false), // phrase 2 (Mixed), remove mapping, no anchor
197  );
198  foreach ($kotobaClearingEvents as $params) {
199  list($cePk, $uploadtreeFk, $cpFk, $hasHighlight) = $params;
200  $this->dbManager->insertInto('clearing_event', 'clearing_event_pk, uploadtree_fk, type_fk, group_fk',
201  array($cePk, $uploadtreeFk, ClearingEventTypes::KOTOBA, $this->groupId), 'insert.kotobaevents');
202  if ($hasHighlight) {
203  $this->dbManager->insertInto('highlight_kotoba', 'clearing_event_fk, cp_fk',
204  array($cePk, $cpFk), 'insert.kotobafinds');
205  }
206  }
207  }
208 
209  private function buildProposals($licProp,$i=0)
210  {
211  foreach ($licProp as $lp) {
212  list($item,$user,$group,$rf,$isRm,$t) = $lp;
213  $this->dbManager->insertInto('clearing_event',
214  'clearing_event_pk, uploadtree_fk, user_fk, group_fk, rf_fk, removed, type_fk, date_added',
215  array($i,$item,$user,$group,$rf,$isRm,1, $this->getMyDate($this->now+$t)));
216  $i++;
217  }
218  }
219 
220  private function buildDecisions($cDec,$j=0)
221  {
222  foreach ($cDec as $cd) {
223  list($item,$user,$group,$type,$t,$scope,$eventIds) = $cd;
224  $this->dbManager->insertInto('clearing_decision',
225  'clearing_decision_pk, uploadtree_fk, pfile_fk, user_fk, group_fk, decision_type, date_added, scope',
226  array($j,$item,$this->items[$item][2],$user,$group,$type, $this->getMyDate($this->now+$t),$scope));
227  foreach ($eventIds as $eId) {
228  $this->dbManager->insertTableRow('clearing_decision_event', array('clearing_decision_fk' => $j, 'clearing_event_fk' => $eId));
229  }
230  $j++;
231  }
232  }
233 
234  function tearDown() : void
235  {
236  $this->testDb = null;
237  $this->dbManager = null;
238  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
239  }
240 
241  private function getMyDate($ts)
242  {
243  return date('Y-m-d H:i:s T',$ts);
244  }
245 
246  public function testRelevantClearingEvents()
247  {
248  $groupId = 701;
249  $this->buildProposals(array(
250  array(301,1,$groupId,401,false,-99),
251  array(301,2,$groupId,402,true,-98),
252  array(301,2,$groupId,401,true,-97)
253  ),$firstEventId=0);
254  $this->buildDecisions(array(
255  array(301,1,$groupId,DecisionTypes::IDENTIFIED,-90,DecisionScopes::REPO,array($firstEventId,$firstEventId+1,$firstEventId+2))
256  ));
257  $itemTreeBounds = M::mock(ItemTreeBounds::class);
258  $itemTreeBounds->shouldReceive('getItemId')->andReturn(301);
259  $itemTreeBounds->shouldReceive('getUploadTreeTableName')->andReturn('uploadtree');
260  $itemTreeBounds->shouldReceive('containsFiles')->andReturn(false);
261  $itemTreeBounds->shouldReceive('getUploadId')->andReturn($this->items[301][0]);
262  $itemTreeBounds->shouldReceive('getLeft')->andReturn($this->items[301][4]);
263  $itemTreeBounds->shouldReceive('getRight')->andReturn($this->items[301][5]);
264  $this->uploadDao->shouldReceive('getGlobalDecisionSettingsFromInfo')
265  ->withArgs([$this->items[301][0]])->andReturn(false);
266 
267  $events1 = $this->clearingDao->getRelevantClearingEvents($itemTreeBounds, $groupId);
268 
269  assertThat($events1, arrayWithSize(2));
270  assertThat($events1, hasKeyInArray(401));
271  assertThat($events1, hasKeyInArray(402));
272  assertThat($events1[401], is(anInstanceOf(ClearingEvent::class)));
273  assertThat($events1[402]->getEventId(), is($firstEventId+1));
274  assertThat($events1[401]->getEventId(), is($firstEventId+2));
275  }
276 
277  function testWip()
278  {
279  $groupId = 701;
280  $this->buildProposals(array(
281  array(301,1,$groupId,401,false,-99),
282  array(301,1,$groupId,402,false,-98),
283  array(301,1,$groupId,401,true,-89),
284  ),$firstEventId=0);
285  $this->buildDecisions(array(
286  array(301,1,$groupId,DecisionTypes::IDENTIFIED,-90,DecisionScopes::REPO,array($firstEventId,$firstEventId+1))
287  ));
288  $watchThis = $this->clearingDao->isDecisionCheck(301, $groupId, DecisionTypes::WIP);
289  assertThat($watchThis,is(FALSE));
290  $watchOther = $this->clearingDao->isDecisionCheck(303, $groupId, DecisionTypes::WIP);
291  assertThat($watchOther,is(FALSE));
292  $this->buildProposals(array(
293  array(301,1,$groupId,403,false,-89),
294  ),$firstEventId+3);
295  $this->clearingDao->markDecisionAsWip(301, 1, $groupId);
296  $watchThisNow = $this->clearingDao->isDecisionCheck(301, $groupId, DecisionTypes::WIP);
297  assertThat($watchThisNow,is(TRUE));
298  $watchOtherNow = $this->clearingDao->isDecisionCheck(303, $groupId, DecisionTypes::WIP);
299  assertThat($watchOtherNow,is(FALSE));
300  }
301 
302  private function collectBulkLicenses($bulks)
303  {
304  $bulkLics = array();
305  foreach ($bulks as $bulk) {
306  if (array_key_exists('removedLicenses', $bulk)) {
307  $bulkLics = array_merge($bulkLics, $bulk['removedLicenses']);
308  }
309  if (array_key_exists('addedLicenses', $bulk)) {
310  $bulkLics = array_merge($bulkLics, $bulk['addedLicenses']);
311  }
312  }
313  return $bulkLics;
314  }
315 
316  public function testBulkHistoryWithoutMatches()
317  {
318  $treeBounds = M::mock(ItemTreeBounds::class);
319  $treeBounds->shouldReceive('getItemId')->andReturn(301);
320  $treeBounds->shouldReceive('getLeft')->andReturn(1);
321  $treeBounds->shouldReceive('getUploadTreeTableName')->andReturn("uploadtree");
322  $treeBounds->shouldReceive('getUploadId')->andReturn(101);
323  $bulks = $this->clearingDao->getBulkHistory($treeBounds, $this->groupId);
324 
325  $bulkMatched = array_map(function($bulk){
326  return $bulk['matched'];
327  }, $bulks);
328  $bulkText = array_map(function($bulk){
329  return $bulk['text'];
330  }, $bulks);
331 
332  assertThat($bulkMatched, arrayContaining(false, false, false, false, false));
333  assertThat($this->collectBulkLicenses($bulks), arrayContaining('FOO', 'BAR', 'BAZ', 'BAZ', 'QUX'));
334  assertThat($bulkText, arrayContaining('TextFOO', 'TextBAR', 'TextBAZ', 'TextBAZ', 'TextQUX'));
335  }
336 
337  public function testBulkHistoryWithoutMatchesFromDifferentFolder()
338  {
339  $treeBounds = M::mock(ItemTreeBounds::class);
340  $treeBounds->shouldReceive('getItemId')->andReturn(305);
341  $treeBounds->shouldReceive('getLeft')->andReturn(4);
342  $treeBounds->shouldReceive('getUploadTreeTableName')->andReturn("uploadtree");
343  $treeBounds->shouldReceive('getUploadId')->andReturn(102);
344  $bulks = $this->clearingDao->getBulkHistory($treeBounds, $this->groupId);
345 
346  $bulkMatched = array_map(function($bulk){
347  return $bulk['matched'];
348  }, $bulks);
349  assertThat($bulkMatched, arrayContaining(false));
350  }
351 
352  public function testBulkHistoryWithAMatch()
353  {
354  $this->insertBulkEvents();
355 
356  $treeBounds = M::mock(ItemTreeBounds::class);
357  $treeBounds->shouldReceive('getItemId')->andReturn(301);
358  $treeBounds->shouldReceive('getLeft')->andReturn(1);
359  $treeBounds->shouldReceive('getUploadTreeTableName')->andReturn("uploadtree");
360  $treeBounds->shouldReceive('getUploadId')->andReturn(101);
361  $bulks = $this->clearingDao->getBulkHistory($treeBounds, $this->groupId);
362 
363  $clearingEventIds = array_map(function($bulk){
364  return $bulk['id'];
365  }, $bulks);
366  $bulkMatched = array_map(function($bulk){
367  return $bulk['matched'];
368  }, $bulks);
369  $bulkLicDirs = array_map(function($bulk){
370  return count($bulk['removedLicenses'])>0;
371  }, $bulks);
372  $bulkTried = array_map(function($bulk){
373  return $bulk['tried'];
374  }, $bulks);
375 
376  assertThat($clearingEventIds, arrayContaining(5001, null, null, 5004, null));
377  assertThat($bulkMatched, arrayContaining(true, false, false, true, false));
378  assertThat($this->collectBulkLicenses($bulks), arrayContaining('FOO', 'BAR', 'BAZ', 'BAZ', 'QUX'));
379  assertThat($bulkLicDirs, arrayContaining(false, false, true, false, true));
380  assertThat($bulkTried, arrayContaining(true, true, true, true, true));
381  }
382 
383  public function testBulkHistoryWithAMatchReturningAlsoNotTried()
384  {
385  $this->insertBulkEvents();
386 
387  $treeBounds = M::mock(ItemTreeBounds::class);
388  $treeBounds->shouldReceive('getItemId')->andReturn(301);
389  $treeBounds->shouldReceive('getLeft')->andReturn(1);
390  $treeBounds->shouldReceive('getUploadTreeTableName')->andReturn("uploadtree");
391  $treeBounds->shouldReceive('getUploadId')->andReturn(101);
392  $bulks = $this->clearingDao->getBulkHistory($treeBounds, $this->groupId, false);
393 
394  $clearingEventIds = array_map(function($bulk){
395  return $bulk['id'];
396  }, $bulks);
397  $bulkMatched = array_map(function($bulk){
398  return $bulk['matched'];
399  }, $bulks);
400  $bulkLicDirs = array_map(function($bulk){
401  return count($bulk['removedLicenses'])>0;
402  }, $bulks);
403  $bulkTried = array_map(function($bulk){
404  return $bulk['tried'];
405  }, $bulks);
406 
407  assertThat($clearingEventIds, arrayContaining(5001, null, null, 5004, null, null));
408  assertThat($bulkMatched, arrayContaining(true, false, false, true, false, false));
409  assertThat($this->collectBulkLicenses($bulks), arrayContaining('FOO', 'BAR', 'BAZ', 'BAZ', 'QUX', 'FOO'));
410  assertThat($bulkLicDirs, arrayContaining(false, false, true, false, true, true));
411  assertThat($bulkTried, arrayContaining(true, true, true, true, true, false));
412  }
413 
423  {
424  $this->insertKotobaEvents();
425 
426  $treeBounds = M::mock(ItemTreeBounds::class);
427  $treeBounds->shouldReceive('getItemId')->andReturn(301);
428  $treeBounds->shouldReceive('getLeft')->andReturn(1);
429  $treeBounds->shouldReceive('getRight')->andReturn(2);
430  $treeBounds->shouldReceive('getUploadTreeTableName')->andReturn("uploadtree");
431  $treeBounds->shouldReceive('getUploadId')->andReturn(101);
432  $phrases = $this->clearingDao->getKotobaHistory($treeBounds, $this->groupId);
433 
434  assertThat($phrases, hasKeyInArray(2));
435  $mixedPhrase = $phrases[2];
436  assertThat($mixedPhrase['text'], is('TextMixed'));
437  assertThat($mixedPhrase['matched'], is(true));
438  assertThat($mixedPhrase['tried'], is(true));
439  assertThat($mixedPhrase['addedLicenses'], arrayContaining('FOO'));
440  assertThat($mixedPhrase['removedLicenses'], arrayContaining('BAR'));
441  }
442 
450  {
451  $this->insertKotobaEvents();
452 
453  // Viewed from item 301 itself: phrase 1 was decided on 302, a sibling,
454  // not on 301 and not inside 301's own (file-sized) range -> absent.
455  $fileBounds = M::mock(ItemTreeBounds::class);
456  $fileBounds->shouldReceive('getItemId')->andReturn(301);
457  $fileBounds->shouldReceive('getLeft')->andReturn(1);
458  $fileBounds->shouldReceive('getRight')->andReturn(2);
459  $fileBounds->shouldReceive('getUploadTreeTableName')->andReturn("uploadtree");
460  $fileBounds->shouldReceive('getUploadId')->andReturn(101);
461  $phrasesFromFile = $this->clearingDao->getKotobaHistory($fileBounds, $this->groupId);
462  assertThat($phrasesFromFile, is(not(hasKeyInArray(1))));
463 
464  // Viewed from the upload root: phrase 1 is tried (decided somewhere in
465  // the subtree) but not matched on the root item itself.
466  $rootBounds = M::mock(ItemTreeBounds::class);
467  $rootBounds->shouldReceive('getItemId')->andReturn(299);
468  $rootBounds->shouldReceive('getLeft')->andReturn(1);
469  $rootBounds->shouldReceive('getRight')->andReturn(4);
470  $rootBounds->shouldReceive('getUploadTreeTableName')->andReturn("uploadtree");
471  $rootBounds->shouldReceive('getUploadId')->andReturn(101);
472  $phrasesFromRoot = $this->clearingDao->getKotobaHistory($rootBounds, $this->groupId);
473  assertThat($phrasesFromRoot, hasKeyInArray(1));
474  assertThat($phrasesFromRoot[1]['matched'], is(false));
475  assertThat($phrasesFromRoot[1]['tried'], is(true));
476  }
477 
487  {
488  $treeBounds = M::mock(ItemTreeBounds::class);
489  $treeBounds->shouldReceive('getItemId')->andReturn(301);
490  $treeBounds->shouldReceive('getLeft')->andReturn(1);
491  $treeBounds->shouldReceive('getRight')->andReturn(2);
492  $treeBounds->shouldReceive('getUploadTreeTableName')->andReturn("uploadtree");
493  $treeBounds->shouldReceive('getUploadId')->andReturn(101);
494  $phrases = $this->clearingDao->getKotobaHistory($treeBounds, $this->groupId);
495 
496  assertThat($phrases, is(emptyArray()));
497  }
498 
499  public function testGetClearedLicenseMultiplicities()
500  {
501  $user = 1;
502  $groupId = 601;
503  $rf = 401;
504  $isRm = false;
505  $t = -10815;
506  $this->buildProposals(array(array(303,$user,$groupId,$rf,$isRm,$t),
507  array(305,$user,$groupId,$rf,$isRm,$t+1)),$eventId=0);
508  $type = DecisionTypes::IDENTIFIED;
509  $scope = DecisionScopes::ITEM;
510  $this->buildDecisions(array(array(303,$user,$groupId,$type,$t,$scope,array($eventId)),
511  array(305,$user,$groupId,$type,$t,$scope,array($eventId+1))));
512  $treeBounds = M::mock(ItemTreeBounds::class);
513 
514  $treeBounds->shouldReceive('getLeft')->andReturn(1);
515  $treeBounds->shouldReceive('getRight')->andReturn(8);
516  $treeBounds->shouldReceive('getUploadTreeTableName')->andReturn("uploadtree");
517  $treeBounds->shouldReceive('getUploadId')->andReturn(102);
518 
519  $this->uploadDao->shouldReceive('getGlobalDecisionSettingsFromInfo')
520  ->withArgs([102])->andReturn(false);
521 
522  $map = $this->clearingDao->getClearedLicenseIdAndMultiplicities($treeBounds, $groupId);
523  assertThat($map, is(array('FOO'=>array('count'=>2,'shortname'=>'FOO','spdx_id'=>'FOO','rf_pk'=>401))));
524  }
525 
526  public function testGetClearedLicenses()
527  {
528  $user = 1;
529  $groupId = 601;
530  $rf = 401;
531  $isRm = false;
532  $t = -10815;
533  $item = 303;
534  $this->buildProposals(array(array($item,$user,$groupId,$rf,$isRm,$t),
535  array($item,$user,$groupId,$rf+1,!$isRm,$t+1)),$eventId=0);
536  $type = DecisionTypes::IDENTIFIED;
537  $scope = DecisionScopes::ITEM;
538  $this->buildDecisions(array( array( $item,$user,$groupId,$type,$t,$scope,array($eventId,$eventId+1) ) ));
539  $treeBounds = M::mock(ItemTreeBounds::class);
540 
541  $treeBounds->shouldReceive('getLeft')->andReturn(1);
542  $treeBounds->shouldReceive('getRight')->andReturn(8);
543  $treeBounds->shouldReceive('getUploadTreeTableName')->andReturn("uploadtree");
544  $treeBounds->shouldReceive('getUploadId')->andReturn(102);
545 
546  $this->uploadDao->shouldReceive('getGlobalDecisionSettingsFromInfo')
547  ->withArgs([102])->andReturn(false);
548 
549  $map = $this->clearingDao->getClearedLicenses($treeBounds, $groupId);
550  assertThat($map, equalTo(array(new LicenseRef($rf,'FOO','foo full', 'FOO'))));
551  }
552 
553 
554  public function testMainLicenseIds()
555  {
556  $this->testDb->createPlainTables(array('upload_clearing_license'));
557  $uploadId = 101;
558  $mainLicIdsInitially = $this->clearingDao->getMainLicenseIds($uploadId, $this->groupId);
559  assertThat($mainLicIdsInitially, is(emptyArray()));
560 
561  $this->clearingDao->makeMainLicense($uploadId,$this->groupId,$licenseId=402);
562  $mainLicIdsAfterAddingOne = $this->clearingDao->getMainLicenseIds($uploadId, $this->groupId);
563  assertThat($mainLicIdsAfterAddingOne, arrayContaining(array($licenseId)));
564 
565  $this->clearingDao->makeMainLicense($uploadId,$this->groupId,$licenseId);
566  $mainLicIdsAfterAddingOneTwice = $this->clearingDao->getMainLicenseIds($uploadId, $this->groupId);
567  assertThat($mainLicIdsAfterAddingOneTwice, is(arrayWithSize(1)));
568 
569  $this->clearingDao->makeMainLicense($uploadId,$this->groupId,$licenseId2=403);
570  $mainLicIdsAfterAddingOther = $this->clearingDao->getMainLicenseIds($uploadId, $this->groupId);
571  assertThat($mainLicIdsAfterAddingOther, arrayContainingInAnyOrder(array($licenseId,$licenseId2)));
572 
573  $this->clearingDao->removeMainLicense($uploadId,$this->groupId,$licenseId2);
574  $mainLicIdsAfterRemovingOne = $this->clearingDao->getMainLicenseIds($uploadId, $this->groupId);
575  assertThat($mainLicIdsAfterRemovingOne, is(arrayWithSize(1)));
576 
577  $this->clearingDao->removeMainLicense($uploadId,$this->groupId,$licenseId2);
578  $mainLicIdAfterRemovingSomethingNotInSet = $this->clearingDao->getMainLicenseIds($uploadId, $this->groupId);
579  assertThat($mainLicIdAfterRemovingSomethingNotInSet, is(arrayWithSize(1)));
580 
581  $this->clearingDao->removeMainLicense($uploadId,$this->groupId+1,$licenseId);
582  $mainLicIdAfterInsertToOtherGroup = $this->clearingDao->getMainLicenseIds($uploadId, $this->groupId);
583  assertThat($mainLicIdAfterInsertToOtherGroup, is(arrayWithSize(1)));
584 
585  $this->clearingDao->removeMainLicense($uploadId+1,$this->groupId,$licenseId);
586  $mainLicIdAfterInsertToOtherUpload = $this->clearingDao->getMainLicenseIds($uploadId, $this->groupId);
587  assertThat($mainLicIdAfterInsertToOtherUpload, is(arrayWithSize(1)));
588  }
589 
590  public function testupdateClearingEvent()
591  {
592  $this->testDb->createSequences(array('clearing_event_clearing_event_pk_seq'));
593  $this->testDb->createConstraints(array('clearing_event_pkey'));
594  $this->dbManager->queryOnce("ALTER TABLE clearing_event ALTER COLUMN clearing_event_pk SET DEFAULT nextval('clearing_event_clearing_event_pk_seq'::regclass)");
595 
596  $this->clearingDao->updateClearingEvent($uploadTreeId=301, $userId=1, $groupId=1, $licenseId=402, $what='comment', $changeCom='abc123');
597  $rowPast = $this->dbManager->getSingleRow('SELECT * FROM clearing_event WHERE uploadtree_fk=$1 AND rf_fk=$2 ORDER BY clearing_event_pk DESC LIMIT 1',array($uploadTreeId,$licenseId),__METHOD__.'beforeReportinfo');
598  assertThat($rowPast['comment'],equalTo($changeCom));
599 
600  $this->clearingDao->updateClearingEvent($uploadTreeId, $userId, $groupId, $licenseId, $what='reportinfo', $changeRep='def456');
601  $rowFuture = $this->dbManager->getSingleRow('SELECT * FROM clearing_event WHERE uploadtree_fk=$1 AND rf_fk=$2 ORDER BY clearing_event_pk DESC LIMIT 1',array($uploadTreeId,$licenseId),__METHOD__.'afterReportinfo');
602  assertThat($rowFuture['comment'],equalTo($changeCom));
603  assertThat($rowFuture['reportinfo'],equalTo($changeRep));
604  }
605 }
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16