48 private $groupId = 601;
51 protected function setUp()
53 $this->uploadDao = M::mock(UploadDao::class);
55 $logger =
new Logger(
'default');
56 $logger->pushHandler(
new ErrorLogHandler());
59 $this->
dbManager = &$this->testDb->getDbManager();
63 $this->testDb->createPlainTables(
66 'clearing_decision_event',
67 'clearing_decision_type',
78 $this->testDb->createInheritedTables();
82 array(
'in_same_group', 2),
83 array(
'in_trusted_group', 3));
84 foreach ($userArray as $ur) {
85 $this->
dbManager->insertInto(
'users',
'user_name, root_folder_fk', $ur);
89 array(401,
'FOO',
'foo full',
'foo text'),
90 array(402,
'BAR',
'bar full',
'bar text'),
91 array(403,
'BAZ',
'baz full',
'baz text'),
92 array(404,
'QUX',
'qux full',
'qux text')
94 foreach ($refArray as $params) {
95 $this->
dbManager->insertInto(
'license_ref',
'rf_pk, rf_shortname, rf_fullname, rf_text', $params, $logStmt =
'insert.ref');
111 $this->items = array(
112 299=>array(101, 299, 0, $modd, 1, 4,
"upload101"),
113 300=>array(102, 300, 0, $modd, 1, 8,
"upload102"),
114 301=>array(101, 301, 201, $modf, 1, 2,
"Afile"),
115 302=>array(101, 302, 202, $modf, 3, 4,
"Bfile"),
116 303=>array(102, 303, 201, $modf, 1, 2,
"Afile"),
117 304=>array(102, 304, 0, $modd, 3, 6,
"A-dir"),
118 305=>array(102, 305, 201, $modf, 4, 5,
"Afile"),
119 306=>array(102, 306, 202, $modf, 7, 8,
"Bfile"),
121 foreach ($this->items as $ur) {
122 $this->
dbManager->insertInto(
'uploadtree',
'upload_fk,uploadtree_pk,pfile_fk,ufile_mode,lft,rgt,ufile_name', $ur);
126 $bulkLicArray = array(
127 array(1, 401,
'TextFOO',
false, 101, 299, $this->groupId),
128 array(2, 402,
'TextBAR',
false, 101, 299, $this->groupId),
129 array(3, 403,
'TextBAZ',
true, 101, 301, $this->groupId),
130 array(4, 403,
'TextBAZ',
false, 101, 299, $this->groupId),
131 array(5, 404,
'TextQUX',
true, 101, 299, $this->groupId),
132 array(6, 401,
'TexxFOO',
true, 101, 302, $this->groupId),
133 array(7, 403,
'TextBAZ',
false, 102, 300, $this->groupId),
134 array(8, 403,
'TextBAZ',
true, 102, 306, $this->groupId)
136 foreach ($bulkLicArray as $params) {
137 $paramsRef = array($params[0], $params[2], $params[4], $params[5], $params[6]);
138 $paramsSet = array($params[0], $params[1], $params[3]);
139 $this->
dbManager->insertInto(
'license_ref_bulk',
'lrb_pk, rf_text, upload_fk, uploadtree_fk, group_fk', $paramsRef,
'insert.bulkref');
140 $this->
dbManager->insertInto(
'license_set_bulk',
'lrb_fk, rf_fk, removing', $paramsSet,
'insert.bulkset');
143 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
146 private function insertBulkEvents()
148 $bulkFindingsArray = array(
156 foreach ($bulkFindingsArray as $params) {
157 $this->
dbManager->insertInto(
'highlight_bulk',
'lrb_fk, clearing_event_fk', $params, $logStmt =
'insert.bulkfinds');
160 $bulkClearingEvents = array(
167 foreach ($bulkClearingEvents as $params) {
168 $this->
dbManager->insertInto(
'clearing_event',
'clearing_event_pk, uploadtree_fk', $params, $logStmt =
'insert.bulkevents');
172 private function buildProposals($licProp,$i=0)
174 foreach ($licProp as $lp) {
175 list($item,$user,$group,$rf,$isRm,$t) = $lp;
176 $this->
dbManager->insertInto(
'clearing_event',
177 'clearing_event_pk, uploadtree_fk, user_fk, group_fk, rf_fk, removed, type_fk, date_added',
178 array($i,$item,$user,$group,$rf,$isRm,1, $this->getMyDate($this->now+$t)));
183 private function buildDecisions($cDec,$j=0)
185 foreach ($cDec as $cd) {
186 list($item,$user,$group,$type,$t,$scope,$eventIds) = $cd;
187 $this->
dbManager->insertInto(
'clearing_decision',
188 'clearing_decision_pk, uploadtree_fk, pfile_fk, user_fk, group_fk, decision_type, date_added, scope',
189 array($j,$item,$this->items[$item][2],$user,$group,$type, $this->getMyDate($this->now+$t),$scope));
190 foreach ($eventIds as $eId) {
191 $this->
dbManager->insertTableRow(
'clearing_decision_event', array(
'clearing_decision_fk' => $j,
'clearing_event_fk' => $eId));
199 $this->testDb = null;
201 $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
204 private function getMyDate($ts)
206 return date(
'Y-m-d H:i:s T',$ts);
209 public function testRelevantClearingEvents()
212 $this->buildProposals(array(
213 array(301,1,$groupId,401,
false,-99),
214 array(301,2,$groupId,402,
true,-98),
215 array(301,2,$groupId,401,
true,-97)
217 $this->buildDecisions(array(
218 array(301,1,$groupId,DecisionTypes::IDENTIFIED,-90,DecisionScopes::REPO,array($firstEventId,$firstEventId+1,$firstEventId+2))
220 $itemTreeBounds = M::mock(ItemTreeBounds::class);
221 $itemTreeBounds->shouldReceive(
'getItemId')->andReturn(301);
222 $itemTreeBounds->shouldReceive(
'getUploadTreeTableName')->andReturn(
'uploadtree');
223 $itemTreeBounds->shouldReceive(
'containsFiles')->andReturn(
false);
224 $itemTreeBounds->shouldReceive(
'getUploadId')->andReturn($this->items[301][0]);
225 $itemTreeBounds->shouldReceive(
'getLeft')->andReturn($this->items[301][4]);
226 $itemTreeBounds->shouldReceive(
'getRight')->andReturn($this->items[301][5]);
228 $events1 = $this->clearingDao->getRelevantClearingEvents($itemTreeBounds, $groupId);
230 assertThat($events1, arrayWithSize(2));
231 assertThat($events1, hasKeyInArray(401));
232 assertThat($events1, hasKeyInArray(402));
233 assertThat($events1[401], is(anInstanceOf(ClearingEvent::class)));
234 assertThat($events1[402]->getEventId(), is($firstEventId+1));
235 assertThat($events1[401]->getEventId(), is($firstEventId+2));
241 $this->buildProposals(array(
242 array(301,1,$groupId,401,
false,-99),
243 array(301,1,$groupId,402,
false,-98),
244 array(301,1,$groupId,401,
true,-89),
246 $this->buildDecisions(array(
247 array(301,1,$groupId,DecisionTypes::IDENTIFIED,-90,DecisionScopes::REPO,array($firstEventId,$firstEventId+1))
249 $watchThis = $this->clearingDao->isDecisionWip(301, $groupId);
250 assertThat($watchThis,is(FALSE));
251 $watchOther = $this->clearingDao->isDecisionWip(303, $groupId);
252 assertThat($watchOther,is(FALSE));
253 $this->buildProposals(array(
254 array(301,1,$groupId,403,
false,-89),
256 $this->clearingDao->markDecisionAsWip(301, 1, $groupId);
257 $watchThisNow = $this->clearingDao->isDecisionWip(301, $groupId);
258 assertThat($watchThisNow,is(TRUE));
259 $watchOtherNow = $this->clearingDao->isDecisionWip(303, $groupId);
260 assertThat($watchOtherNow,is(FALSE));
263 private function collectBulkLicenses($bulks)
266 foreach ($bulks as $bulk) {
267 if (array_key_exists(
'removedLicenses', $bulk)) {
268 $bulkLics = array_merge($bulkLics, $bulk[
'removedLicenses']);
270 if (array_key_exists(
'addedLicenses', $bulk)) {
271 $bulkLics = array_merge($bulkLics, $bulk[
'addedLicenses']);
277 public function testBulkHistoryWithoutMatches()
279 $treeBounds = M::mock(ItemTreeBounds::class);
280 $treeBounds->shouldReceive(
'getItemId')->andReturn(301);
281 $treeBounds->shouldReceive(
'getLeft')->andReturn(1);
282 $treeBounds->shouldReceive(
'getUploadTreeTableName')->andReturn(
"uploadtree");
283 $treeBounds->shouldReceive(
'getUploadId')->andReturn(101);
284 $bulks = $this->clearingDao->getBulkHistory($treeBounds, $this->groupId);
286 $bulkMatched = array_map(
function($bulk){
287 return $bulk[
'matched'];
289 $bulkText = array_map(
function($bulk){
290 return $bulk[
'text'];
293 assertThat($bulkMatched, arrayContaining(
false,
false,
false,
false,
false));
294 assertThat($this->collectBulkLicenses($bulks), arrayContaining(
'FOO',
'BAR',
'BAZ',
'BAZ',
'QUX'));
295 assertThat($bulkText, arrayContaining(
'TextFOO',
'TextBAR',
'TextBAZ',
'TextBAZ',
'TextQUX'));
298 public function testBulkHistoryWithoutMatchesFromDifferentFolder()
300 $treeBounds = M::mock(ItemTreeBounds::class);
301 $treeBounds->shouldReceive(
'getItemId')->andReturn(305);
302 $treeBounds->shouldReceive(
'getLeft')->andReturn(4);
303 $treeBounds->shouldReceive(
'getUploadTreeTableName')->andReturn(
"uploadtree");
304 $treeBounds->shouldReceive(
'getUploadId')->andReturn(102);
305 $bulks = $this->clearingDao->getBulkHistory($treeBounds, $this->groupId);
307 $bulkMatched = array_map(
function($bulk){
308 return $bulk[
'matched'];
310 assertThat($bulkMatched, arrayContaining(
false));
313 public function testBulkHistoryWithAMatch()
315 $this->insertBulkEvents();
317 $treeBounds = M::mock(ItemTreeBounds::class);
318 $treeBounds->shouldReceive(
'getItemId')->andReturn(301);
319 $treeBounds->shouldReceive(
'getLeft')->andReturn(1);
320 $treeBounds->shouldReceive(
'getUploadTreeTableName')->andReturn(
"uploadtree");
321 $treeBounds->shouldReceive(
'getUploadId')->andReturn(101);
322 $bulks = $this->clearingDao->getBulkHistory($treeBounds, $this->groupId);
324 $clearingEventIds = array_map(
function($bulk){
327 $bulkMatched = array_map(
function($bulk){
328 return $bulk[
'matched'];
330 $bulkLicDirs = array_map(
function($bulk){
331 return count($bulk[
'removedLicenses'])>0;
333 $bulkTried = array_map(
function($bulk){
334 return $bulk[
'tried'];
337 assertThat($clearingEventIds, arrayContaining(5001, null, null, 5004, null));
338 assertThat($bulkMatched, arrayContaining(
true,
false,
false,
true,
false));
339 assertThat($this->collectBulkLicenses($bulks), arrayContaining(
'FOO',
'BAR',
'BAZ',
'BAZ',
'QUX'));
340 assertThat($bulkLicDirs, arrayContaining(
false,
false,
true,
false,
true));
341 assertThat($bulkTried, arrayContaining(
true,
true,
true,
true,
true));
344 public function testBulkHistoryWithAMatchReturningAlsoNotTried()
346 $this->insertBulkEvents();
348 $treeBounds = M::mock(ItemTreeBounds::class);
349 $treeBounds->shouldReceive(
'getItemId')->andReturn(301);
350 $treeBounds->shouldReceive(
'getLeft')->andReturn(1);
351 $treeBounds->shouldReceive(
'getUploadTreeTableName')->andReturn(
"uploadtree");
352 $treeBounds->shouldReceive(
'getUploadId')->andReturn(101);
353 $bulks = $this->clearingDao->getBulkHistory($treeBounds, $this->groupId,
false);
355 $clearingEventIds = array_map(
function($bulk){
358 $bulkMatched = array_map(
function($bulk){
359 return $bulk[
'matched'];
361 $bulkLicDirs = array_map(
function($bulk){
362 return count($bulk[
'removedLicenses'])>0;
364 $bulkTried = array_map(
function($bulk){
365 return $bulk[
'tried'];
368 assertThat($clearingEventIds, arrayContaining(5001, null, null, 5004, null, null));
369 assertThat($bulkMatched, arrayContaining(
true,
false,
false,
true,
false,
false));
370 assertThat($this->collectBulkLicenses($bulks), arrayContaining(
'FOO',
'BAR',
'BAZ',
'BAZ',
'QUX',
'FOO'));
371 assertThat($bulkLicDirs, arrayContaining(
false,
false,
true,
false,
true,
true));
372 assertThat($bulkTried, arrayContaining(
true,
true,
true,
true,
true,
false));
375 public function testGetClearedLicenseMultiplicities()
382 $this->buildProposals(array(array(303,$user,$groupId,$rf,$isRm,$t),
383 array(305,$user,$groupId,$rf,$isRm,$t+1)),$eventId=0);
384 $type = DecisionTypes::IDENTIFIED;
385 $scope = DecisionScopes::ITEM;
386 $this->buildDecisions(array(array(303,$user,$groupId,$type,$t,$scope,array($eventId)),
387 array(305,$user,$groupId,$type,$t,$scope,array($eventId+1))));
388 $treeBounds = M::mock(ItemTreeBounds::class);
390 $treeBounds->shouldReceive(
'getLeft')->andReturn(1);
391 $treeBounds->shouldReceive(
'getRight')->andReturn(8);
392 $treeBounds->shouldReceive(
'getUploadTreeTableName')->andReturn(
"uploadtree");
393 $treeBounds->shouldReceive(
'getUploadId')->andReturn(102);
395 $map = $this->clearingDao->getClearedLicenseIdAndMultiplicities($treeBounds, $groupId);
396 assertThat($map, is(array(
'FOO'=>array(
'count'=>2,
'shortname'=>
'FOO',
'rf_pk'=>401))));
399 public function testGetClearedLicenses()
407 $this->buildProposals(array(array($item,$user,$groupId,$rf,$isRm,$t),
408 array($item,$user,$groupId,$rf+1,!$isRm,$t+1)),$eventId=0);
409 $type = DecisionTypes::IDENTIFIED;
410 $scope = DecisionScopes::ITEM;
411 $this->buildDecisions(array( array( $item,$user,$groupId,$type,$t,$scope,array($eventId,$eventId+1) ) ));
412 $treeBounds = M::mock(ItemTreeBounds::class);
414 $treeBounds->shouldReceive(
'getLeft')->andReturn(1);
415 $treeBounds->shouldReceive(
'getRight')->andReturn(8);
416 $treeBounds->shouldReceive(
'getUploadTreeTableName')->andReturn(
"uploadtree");
417 $treeBounds->shouldReceive(
'getUploadId')->andReturn(102);
419 $map = $this->clearingDao->getClearedLicenses($treeBounds, $groupId);
420 assertThat($map, equalTo(array(
new LicenseRef($rf,
'FOO',
'foo full'))));
424 public function testMainLicenseIds()
426 $this->testDb->createPlainTables(array(
'upload_clearing_license'));
428 $mainLicIdsInitially = $this->clearingDao->getMainLicenseIds($uploadId, $this->groupId);
429 assertThat($mainLicIdsInitially, is(emptyArray()));
431 $this->clearingDao->makeMainLicense($uploadId,$this->groupId,$licenseId=402);
432 $mainLicIdsAfterAddingOne = $this->clearingDao->getMainLicenseIds($uploadId, $this->groupId);
433 assertThat($mainLicIdsAfterAddingOne, arrayContaining(array($licenseId)));
435 $this->clearingDao->makeMainLicense($uploadId,$this->groupId,$licenseId);
436 $mainLicIdsAfterAddingOneTwice = $this->clearingDao->getMainLicenseIds($uploadId, $this->groupId);
437 assertThat($mainLicIdsAfterAddingOneTwice, is(arrayWithSize(1)));
439 $this->clearingDao->makeMainLicense($uploadId,$this->groupId,$licenseId2=403);
440 $mainLicIdsAfterAddingOther = $this->clearingDao->getMainLicenseIds($uploadId, $this->groupId);
441 assertThat($mainLicIdsAfterAddingOther, arrayContainingInAnyOrder(array($licenseId,$licenseId2)));
443 $this->clearingDao->removeMainLicense($uploadId,$this->groupId,$licenseId2);
444 $mainLicIdsAfterRemovingOne = $this->clearingDao->getMainLicenseIds($uploadId, $this->groupId);
445 assertThat($mainLicIdsAfterRemovingOne, is(arrayWithSize(1)));
447 $this->clearingDao->removeMainLicense($uploadId,$this->groupId,$licenseId2);
448 $mainLicIdAfterRemovingSomethingNotInSet = $this->clearingDao->getMainLicenseIds($uploadId, $this->groupId);
449 assertThat($mainLicIdAfterRemovingSomethingNotInSet, is(arrayWithSize(1)));
451 $this->clearingDao->removeMainLicense($uploadId,$this->groupId+1,$licenseId);
452 $mainLicIdAfterInsertToOtherGroup = $this->clearingDao->getMainLicenseIds($uploadId, $this->groupId);
453 assertThat($mainLicIdAfterInsertToOtherGroup, is(arrayWithSize(1)));
455 $this->clearingDao->removeMainLicense($uploadId+1,$this->groupId,$licenseId);
456 $mainLicIdAfterInsertToOtherUpload = $this->clearingDao->getMainLicenseIds($uploadId, $this->groupId);
457 assertThat($mainLicIdAfterInsertToOtherUpload, is(arrayWithSize(1)));
460 public function testupdateClearingEvent()
462 $this->testDb->createSequences(array(
'clearing_event_clearing_event_pk_seq'));
463 $this->testDb->createConstraints(array(
'clearing_event_pkey'));
464 $this->
dbManager->queryOnce(
"ALTER TABLE clearing_event ALTER COLUMN clearing_event_pk SET DEFAULT nextval('clearing_event_clearing_event_pk_seq'::regclass)");
466 $this->clearingDao->updateClearingEvent($uploadTreeId=301, $userId=1, $groupId=1, $licenseId=402, $what=
'comment', $changeCom=
'abc123');
467 $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');
468 assertThat($rowPast[
'comment'],equalTo($changeCom));
470 $this->clearingDao->updateClearingEvent($uploadTreeId, $userId, $groupId, $licenseId, $what=
'reportinfo', $changeRep=
'def456');
471 $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');
472 assertThat($rowFuture[
'comment'],equalTo($changeCom));
473 assertThat($rowFuture[
'reportinfo'],equalTo($changeRep));
fo_dbManager * dbManager
fo_dbManager object