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