public function testAddGroup() { $this->dbManager->queryOnce('CREATE TABLE groups (group_pk integer NOT NULL PRIMARY KEY, group_name varchar(64))'); $this->testDb->insertData(array('groups')); $groupId = $this->userDao->addGroup($groupName = 'newGroup'); $row = $this->dbManager->getSingleRow('SELECT group_name FROM groups WHERE group_pk=$1', array($groupId)); assertThat($row['group_name'], equalTo($groupName)); }
public function testGetAgentFileLicenseMatchesWithLicenseMapping() { $this->testDb->createPlainTables(array('license_ref', 'uploadtree', 'license_file', 'agent', 'license_map')); $this->testDb->insertData_license_ref(); $lic0 = $this->dbManager->getSingleRow("Select * from license_ref limit 1", array(), __METHOD__ . '.anyLicense'); $licRefId = $lic0['rf_pk']; $licenseFileId = 1; $pfileId = 42; $agentId = 23; $matchPercent = 50; $uploadtreeId = 512; $uploadID = 123; $left = 2009; $right = 2014; $agentName = "fake"; $agentRev = 1; $lic1 = $this->dbManager->getSingleRow("SELECT * FROM license_ref WHERE rf_pk!=\$1 LIMIT 1", array($licRefId), __METHOD__ . '.anyOtherLicense'); $licVarId = $lic1['rf_pk']; $mydate = "'2014-06-04 14:01:30.551093+02'"; $this->dbManager->insertTableRow('license_map', array('license_map_pk' => 0, 'rf_fk' => $licVarId, 'rf_parent' => $licRefId, 'usage' => LicenseMap::CONCLUSION)); $this->dbManager->queryOnce("INSERT INTO license_file (fl_pk, rf_fk, agent_fk, rf_match_pct, rf_timestamp, pfile_fk)\n VALUES ({$licenseFileId}, {$licVarId}, {$agentId}, {$matchPercent}, {$mydate}, {$pfileId})"); $this->dbManager->queryOnce("INSERT INTO uploadtree (uploadtree_pk, upload_fk, pfile_fk, lft, rgt)\n VALUES ({$uploadtreeId}, {$uploadID}, {$pfileId}, {$left}, {$right})"); $stmt = __METHOD__ . '.insert.agent'; $this->dbManager->prepare($stmt, "INSERT INTO agent (agent_pk, agent_name, agent_rev, agent_enabled) VALUES (\$1,\$2,\$3,\$4)"); $this->dbManager->execute($stmt, array($agentId, $agentName, $agentRev, 'true')); $licDao = new LicenseDao($this->dbManager); $itemTreeBounds = new ItemTreeBounds($uploadtreeId, "uploadtree", $uploadID, $left, $right); $matches = $licDao->getAgentFileLicenseMatches($itemTreeBounds, LicenseMap::CONCLUSION); $licenseRef = new LicenseRef($licRefId, $lic0['rf_shortname'], $lic0['rf_fullname']); $agentRef = new AgentRef($agentId, $agentName, $agentRev); $expected = array(new LicenseMatch($pfileId, $licenseRef, $agentRef, $licenseFileId, $matchPercent)); assertThat($matches, equalTo($expected)); $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore); }
private function runnerDeciderRealShouldMakeNoDecisionForIrrelevantFiles($runner) { $this->setUpTables(); $this->setUpRepo(); $monkAgentId = 5; $licenseRef1 = $this->licenseDao->getLicenseByShortName("GPL-3.0")->getRef(); $licId1 = $licenseRef1->getId(); $pfile = 4; $jobId = 16; $groupId = 2; $userId = 2; $itemId = 7; $itemTreeBounds = new ItemTreeBounds($itemId, 'uploadtree_a', $uploadId = 1, 15, 16); $this->dbManager->queryOnce("DELETE FROM license_file"); /* insert NoLicenseKnown decisions */ $this->dbManager->queryOnce("INSERT INTO clearing_decision (clearing_decision_pk, uploadtree_fk, pfile_fk, user_fk, group_fk, decision_type, scope, date_added)" . " VALUES (2, {$itemId}, {$pfile}, {$userId}, {$groupId}, " . DecisionTypes::IRRELEVANT . ", " . DecisionScopes::ITEM . ", '2015-05-04 11:43:18.276425+02')"); $lastDecision = $this->clearingDao->getRelevantClearingDecision($itemTreeBounds, $groupId); $lastClearingId = $lastDecision->getClearingId(); $this->dbManager->queryOnce("INSERT INTO license_file (fl_pk,rf_fk,pfile_fk,agent_fk) VALUES(12222,{$licId1},{$pfile},{$monkAgentId})"); $this->dbManager->queryOnce("INSERT INTO jobqueue (jq_pk, jq_job_fk, jq_type, jq_args, jq_starttime, jq_endtime, jq_endtext, jq_end_bits, jq_schedinfo, jq_itemsprocessed, jq_log, jq_runonpfile, jq_host, jq_cmd_args)" . " VALUES ({$jobId}, 2, 'decider', '2', '2015-06-07 09:57:27.718312+00', NULL, '', 0, NULL, 6, NULL, NULL, NULL, '-r8')"); list($success, $output, $retCode) = $runner->run($uploadId, $userId, $groupId, $jobId, ''); $this->assertTrue($success, 'cannot run runner'); $this->assertEquals($retCode, 0, 'decider failed (did you make test?): ' . $output); $newDecision = $this->clearingDao->getRelevantClearingDecision($itemTreeBounds, $groupId); assertThat($newDecision->getClearingId(), equalTo($lastClearingId)); $this->rmRepo(); }
protected function tearDown() { $this->dbManager->queryOnce("drop table " . $this->agentName . AgentDao::ARS_TABLE_SUFFIX); $this->dbManager->queryOnce("drop table " . $this->otherAgentName . AgentDao::ARS_TABLE_SUFFIX); $this->dbManager = null; $this->testDb = null; M::close(); }
/** @group Functional */ public function testReportForSpecialUploadtreeTable() { $this->setUpTables(); $this->setUpRepo(); $uploadId = 1; $this->dbManager->queryOnce("ALTER TABLE uploadtree_a RENAME TO uploadtree_{$uploadId}", __METHOD__ . '.alterUploadtree'); $this->dbManager->getSingleRow("UPDATE upload SET uploadtree_tablename=\$1 WHERE upload_pk=\$2", array("uploadtree_{$uploadId}", $uploadId), __METHOD__ . '.alterUpload'); $this->runAndTestReport($uploadId); }
public function testGetUploadHashes() { $this->testDb->createPlainTables(array('pfile')); $this->dbManager->queryOnce('ALTER TABLE uploadtree RENAME TO uploadtree_a'); $this->testDb->insertData(array('uploadtree_a', 'pfile')); // (pfile_pk, pfile_md5, pfile_sha1, pfile_size) := (4, '59CACDFCE5051CD8A1D8A1F2DCCE40A5', '04621571BCBABCE75C4DD1C6445B87DEC0995734', 12320); $hashes = $this->treeDao->getItemHashes(7, 'uploadtree_a'); assertThat($hashes, equalTo(array('md5' => '59CACDFCE5051CD8A1D8A1F2DCCE40A5', 'sha1' => '04621571BCBABCE75C4DD1C6445B87DEC0995734'))); }
function applyTables() { if (empty($this->schema['TABLE'])) { return; } foreach ($this->schema['TABLE'] as $table => $columns) { if (empty($table)) { continue; } if (!DB_TableExists($table)) { $sql = "CREATE TABLE \"{$table}\" ()"; $this->applyOrEchoOnce($sql, $stmt = __METHOD__ . $table); } foreach ($columns as $column => $modification) { if ($this->currSchema['TABLE'][$table][$column]['ADD'] != $modification['ADD']) { $rename = ""; if (DB_ColExists($table, $column)) { /* The column exists, but it looks different! Solution: Delete the column! */ $rename = $column . '_old'; $sql = "ALTER TABLE \"{$table}\" RENAME COLUMN \"{$column}\" TO \"{$rename}\""; $this->applyOrEchoOnce($sql); } $sql = $modification['ADD']; if ($this->debug) { print "{$sql}\n"; } else { // Add the new column which sets the default value $this->dbman->queryOnce($sql); } if (!empty($rename)) { /* copy over the old data */ $this->applyOrEchoOnce($sql = "UPDATE \"{$table}\" SET \"{$column}\" = \"{$rename}\""); $this->applyOrEchoOnce($sql = "ALTER TABLE \"{$table}\" DROP COLUMN \"{$rename}\""); } } if ($this->currSchema['TABLE'][$table][$column]['ALTER'] != $modification['ALTER'] && isset($modification['ALTER'])) { $sql = $modification['ALTER']; if ($this->debug) { print "{$sql}\n"; } else { if (!empty($sql)) { $this->dbman->queryOnce($sql); } } } if ($this->currSchema['TABLE'][$table][$column]['DESC'] != $modification['DESC']) { $sql = empty($modification['DESC']) ? "COMMENT ON COLUMN \"{$table}\".\"{$column}\" IS ''" : $modification['DESC']; $this->applyOrEchoOnce($sql, $stmt = __METHOD__ . "{$table}.{$column}.comment"); } } } }
public function testupdateClearingEvent() { $this->testDb->createSequences(array('clearing_event_clearing_event_pk_seq')); $this->testDb->createConstraints(array('clearing_event_pkey')); $this->dbManager->queryOnce("ALTER TABLE clearing_event ALTER COLUMN clearing_event_pk SET DEFAULT nextval('clearing_event_clearing_event_pk_seq'::regclass)"); $this->clearingDao->updateClearingEvent($uploadTreeId = 301, $userId = 1, $groupId = 1, $licenseId = 402, $what = 'comment', $changeCom = 'abc123'); $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'); assertThat($rowPast['comment'], equalTo($changeCom)); $this->clearingDao->updateClearingEvent($uploadTreeId, $userId, $groupId, $licenseId, $what = 'reportinfo', $changeRep = 'def456'); $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'); assertThat($rowFuture['comment'], equalTo($changeCom)); assertThat($rowFuture['reportinfo'], equalTo($changeRep)); }
public function testMyJobs() { $groupId = 2; $GLOBALS['SysConf']['auth'][Auth::GROUP_ID] = $groupId; $GLOBALS['SysConf']['auth'][Auth::USER_ID] = 1; $this->uploadPermissionDao->shouldReceive('isAccessible')->withArgs(array(anything(), $groupId))->andReturnUsing(function ($upload, $group) { return $upload == 1 || $upload == 2 || $upload == 4; }); $testOurJobs = $this->showJobsDao->myJobs(true); assertThat($testOurJobs, is(arrayContainingInAnyOrder($this->job_pks))); $testMyJobs = $this->showJobsDao->myJobs(false); assertThat($testMyJobs, equalTo(array(1))); $this->dbManager->queryOnce("UPDATE job SET job_queued=job_queued-INTERVAL '30 days' WHERE job_pk=1"); $this->dbManager->prepare(__METHOD__ . 'insert.perm_upload', "INSERT INTO perm_upload (perm_upload_pk, perm, upload_fk, group_fk) VALUES (\$1, \$2, \$3, \$4)"); $testOutdatedJobs = $this->showJobsDao->myJobs(true); assertThat($testOutdatedJobs, equalTo(array(2))); }
/** * @param array $tableList * @param bool $invert */ public function alterTables($tableList, $invert = FALSE) { $coreSchemaFile = $this->dirnameRec(__FILE__, 4) . '/www/ui/core-schema.dat'; $Schema = array(); require $coreSchemaFile; $attributeKey = "ALTER"; foreach ($Schema['TABLE'] as $tableName => $tableCols) { if ($invert ^ !in_array($tableName, $tableList)) { continue; } foreach ($tableCols as $attributes) { if (array_key_exists($attributeKey, $attributes)) { $this->dbManager->queryOnce($attributes[$attributeKey]); } } } }
private function runnerReuserScanWithARepoClearingEnhanced($runner) { $this->setUpTables(); $this->setUpRepo(); $originallyClearedItemId = 23; /* upload 3 in the test db is the same as upload 2 -> items 13-24 in upload 2 correspond to 33-44 */ $reusingUploadItemShift = 20; $this->dbManager->queryOnce("UPDATE uploadtree_a SET pfile_fk=351 WHERE uploadtree_pk={$originallyClearedItemId}+{$reusingUploadItemShift}", __METHOD__ . '.minorChange'); $this->uploadDao->addReusedUpload($uploadId = 3, $reusedUpload = 2, $this->groupId, $this->groupId, $reuseMode = 1); $repoPath = $this->testDb->getFossSysConf() . '/repo/files/'; $this->treeDao->shouldReceive('getRepoPathOfPfile')->with(4)->andReturn($repoPath . '04621571bcbabce75c4dd1c6445b87dec0995734.59cacdfce5051cd8a1d8a1f2dcce40a5.12320'); $this->treeDao->shouldReceive('getRepoPathOfPfile')->with(351)->andReturn($repoPath . 'c518ce1658140b65fa0132ad1130cb91512416bf.8e913e594d24ff3aeabe350107d97815.35829'); list($clearingLicense1, $clearingLicense2, $addedEventIds) = $this->insertDecisionFromTwoEvents(DecisionScopes::REPO, $originallyClearedItemId); $clearingLicenses = array($clearingLicense1, $clearingLicense2); list($success, $output, $retCode) = $runner->run($uploadId, $this->userId, $this->groupId); $this->assertTrue($success, 'cannot run runner'); $this->assertEquals($retCode, 0, 'reuser failed: ' . $output); $newUploadClearings = $this->getFilteredClearings($uploadId, $this->groupId); $potentiallyReusableClearings = $this->getFilteredClearings($reusedUpload, $this->groupId); assertThat($newUploadClearings, is(arrayWithSize(1))); assertThat($potentiallyReusableClearings, is(arrayWithSize(1))); /** @var ClearingDecision */ $potentiallyReusableClearing = $potentiallyReusableClearings[0]; /** @var ClearingDecision */ $newClearing = $newUploadClearings[0]; /* they are actually the same ClearingDecision * only sameFolder and sameUpload are different */ assertThat($newClearing, not(equalTo($potentiallyReusableClearing))); assertThat($newClearing->getClearingLicenses(), arrayContainingInAnyOrder($clearingLicenses)); assertThat($newClearing->getType(), equalTo($potentiallyReusableClearing->getType())); assertThat($newClearing->getScope(), equalTo($potentiallyReusableClearing->getScope())); assertThat($newClearing->getUploadTreeId(), equalTo($potentiallyReusableClearing->getUploadTreeId() + $reusingUploadItemShift)); /* reuser should have not created a correct local event history */ $bounds = $this->uploadDao->getItemTreeBounds($originallyClearedItemId + $reusingUploadItemShift); $newEvents = $this->clearingDao->getRelevantClearingEvents($bounds, $this->groupId); assertThat($newEvents, is(arrayWithSize(count($clearingLicenses)))); /** @var ClearingEvent $newEvent */ foreach ($newEvents as $newEvent) { assertThat($newEvent->getEventId(), anyOf($addedEventIds)); assertThat($newEvent->getClearingLicense(), anyOf($clearingLicenses)); } $this->rmRepo(); }
public function testGetFatItem() { $this->prepareUploadTree($this->getTestFileStructure()); $isContainer = 536888320; $itemM1a = 13655; $this->prepareUploadTree(array(array($itemM1a, 3655, 32, 0, $isContainer, 39 + 0, 40 - 0, 'M1a'))); $this->dbManager->queryOnce('UPDATE uploadtree SET realparent=parent WHERE ufile_mode&(1<<28)=0', __METHOD__ . '.fixRealparent'); $fatA = $this->uploadDao->getFatItemId($itemA = 3653, 32, 'uploadtree'); assertThat($fatA, equalTo($itemA)); $fatB = $this->uploadDao->getFatItemId($itemBEmpty = 3663, 32, 'uploadtree'); assertThat($fatB, equalTo($itemBEmpty)); $fatD = $this->uploadDao->getFatItemId($itemDFolder = 3682, 32, 'uploadtree'); assertThat($fatD, equalTo($itemDFolder)); $fatL1 = $this->uploadDao->getFatItemId($itemL1ToFolder = 3666, 32, 'uploadtree'); assertThat($fatL1, equalTo(3667)); $fatL2 = $this->uploadDao->getFatItemId($itemL2ToItem = 3664, 32, 'uploadtree'); assertThat($fatL2, equalTo(3665)); $fatM = $this->uploadDao->getFatItemId(3654, 32, 'uploadtree'); assertThat($fatM, equalTo($itemM1a)); }
private function runnerDeciderScanWithForceDecision($runner) { $this->setUpTables(); $this->setUpRepo(); $jobId = 42; $licenseRef1 = $this->licenseDao->getLicenseByShortName("GPL-3.0")->getRef(); $licenseRef2 = $this->licenseDao->getLicenseByShortName("3DFX")->getRef(); $agentLicId = $this->licenseDao->getLicenseByShortName("Adaptec")->getRef()->getId(); $addedLicenses = array($licenseRef1, $licenseRef2); assertThat($addedLicenses, not(arrayContaining(null))); $agentId = 5; $pfile = 4; $this->dbManager->queryOnce("INSERT INTO license_file (fl_pk,rf_fk,pfile_fk,agent_fk) VALUES(12222,{$agentLicId},{$pfile},{$agentId})"); $itemTreeBounds = $this->uploadDao->getItemTreeBounds($itemId = 23); assertThat($this->agentLicenseEventProcessor->getScannerEvents($itemTreeBounds), is(not(emptyArray()))); $eventId1 = $this->clearingDao->insertClearingEvent($itemId, $userId = 2, $groupId = 3, $licenseRef1->getId(), false); $eventId2 = $this->clearingDao->insertClearingEvent($itemId, 5, $groupId, $licenseRef2->getId(), true); $this->dbManager->queryOnce("UPDATE clearing_event SET job_fk={$jobId}"); $addedEventIds = array($eventId1, $eventId2); list($success, $output, $retCode) = $runner->run($uploadId = 2, $userId, $groupId, $jobId, $args = "-k1"); $this->assertTrue($success, 'cannot run runner'); $this->assertEquals($retCode, 0, 'decider failed: ' . $output); assertThat($this->getHeartCount($output), equalTo(1)); $uploadBounds = $this->uploadDao->getParentItemBounds($uploadId); $decisions = $this->clearingDao->getFileClearingsFolder($uploadBounds, $groupId); assertThat($decisions, is(arrayWithSize(1))); /** @var ClearingDecision $deciderMadeDecision */ $deciderMadeDecision = $decisions[0]; $eventIds = array(); foreach ($deciderMadeDecision->getClearingEvents() as $event) { $eventIds[] = $event->getEventId(); } assertThat($eventIds, arrayValue($addedEventIds[0])); assertThat($eventIds, arrayValue($addedEventIds[1])); assertThat($eventIds, arrayWithSize(1 + count($addedEventIds))); $this->rmRepo(); }
public function createArsTable($agentName) { $tableName = $this->getArsTableName($agentName); $this->dbManager->queryOnce("CREATE TABLE " . $tableName . "() INHERITS(ars_master);\n ALTER TABLE ONLY " . $tableName . " ADD CONSTRAINT " . $tableName . "_agent_fk_fkc FOREIGN KEY (agent_fk) REFERENCES agent(agent_pk);\n ALTER TABLE ONLY " . $tableName . " ADD CONSTRAINT " . $tableName . "_upload_fk_fkc FOREIGN KEY (upload_fk) REFERENCES upload(upload_pk) ON DELETE CASCADE", __METHOD__); }