8 namespace Fossology\Lib\Dao;
26 private $uploadPermissionDao;
28 private $job_pks = array(2,1);
30 protected function setUp() :
void
33 $this->
dbManager = &$this->testDb->getDbManager();
35 $this->testDb->createPlainTables(
44 $this->testDb->createInheritedTables(array(
'uploadtree_a'));
46 $uploadArray = array(array(
'upload_pk'=>1,
'uploadtree_tablename'=>
'uploadtree'),
47 array(
'upload_pk'=>2,
'uploadtree_tablename'=>
'uploadtree_a'));
48 foreach ($uploadArray as $uploadEntry) {
49 $this->
dbManager->insertTableRow(
'upload', $uploadEntry);
52 $this->
dbManager->prepare($stmt =
'insert.job',
53 "INSERT INTO job (job_pk, job_queued, job_name, job_upload_fk, job_user_fk) VALUES ($1, $2, $3, $4, $5)");
54 $jobArray = array(array(1,date(
'c',time()-5),
"FCKeditor_2.6.4.zip", 1,1 ),
55 array(2,date(
'c'),
"zlib_1.2.8.zip", 2,2));
56 foreach ($jobArray as $uploadEntry) {
60 $logger = M::mock(
'Monolog\Logger');
61 $logger->shouldReceive(
'debug');
62 $this->uploadPermissionDao = M::mock(
'Fossology\Lib\Dao\UploadPermissionDao');
63 $this->uploadDao =
new UploadDao($this->
dbManager, $logger, $this->uploadPermissionDao);
66 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
69 protected function tearDown() :
void
71 $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
78 public function testUploads2Jobs()
83 $this->uploadPermissionDao->shouldReceive(
'isAccessible')->withArgs(array(anything(),$groupId))
84 ->andReturnUsing(
function($upload,$group)
86 return ($upload==1 || $upload==2 || $upload==3 || $upload==4 || $upload==5);
89 $jobs = array(3=>2, 4=>3, 5=>5, 6=>8%6, 7=>13%6, 8=>21%6);
90 foreach ($jobs as $jobId => $jobUpload) {
91 $this->
dbManager->insertTableRow(
'job', array(
'job_pk' => $jobId,
'job_upload_fk' => $jobUpload));
93 $jobsWithoutUpload = $this->showJobsDao->uploads2Jobs(array());
94 assertThat($jobsWithoutUpload, is(emptyArray()));
95 $jobsWithUploadIdOne = $this->showJobsDao->uploads2Jobs(array(1));
96 assertThat($jobsWithUploadIdOne, equalTo(array(array(1,7),0)));
97 $jobsAtAll = $this->showJobsDao->uploads2Jobs(array(1,2,3,4,5));
98 assertThat($jobsAtAll, equalTo(array(array(1,7, 2,3,6, 4,8, 5),0)));
99 $jobsWithUploadFour = $this->showJobsDao->uploads2Jobs(array(4));
100 assertThat($jobsWithUploadFour[0], is(emptyArray()));
103 public function testUploads2JobsPaged()
109 $this->uploadPermissionDao->shouldReceive(
'isAccessible')->withArgs(array(anything(),$groupId))
110 ->andReturnUsing(
function($upload,$group)
115 $jobs = array_combine(range(3,13),range(3,13));
116 foreach ($jobs as $jobId => $jobUpload) {
117 $this->
dbManager->insertTableRow(
'job', array(
'job_pk' => $jobId,
'job_upload_fk' => $jobUpload));
120 $jobsPage1 = $this->showJobsDao->uploads2Jobs(range(1,17),0);
121 assertThat($jobsPage1[0], arrayWithSize(10));
122 assertThat($jobsPage1[1], is(1));
123 $jobsPage2 = $this->showJobsDao->uploads2Jobs(array_combine(range(10,16),range(11,17)),1);
124 assertThat($jobsPage2[0], arrayWithSize(3));
125 assertThat($jobsPage2[1], is(0));
126 $jobsPage3 = $this->showJobsDao->uploads2Jobs(array(),2);
127 assertThat($jobsPage3, arrayWithSize(0));
131 public function testgetJobName()
133 $testJobName = $this->showJobsDao->getJobName(1);
134 assertThat($testJobName, equalTo(
"FCKeditor_2.6.4.zip"));
136 $testJobNameIfNothingQueued = $this->showJobsDao->getJobName($uploadId = 3);
137 assertThat($testJobNameIfNothingQueued, equalTo($uploadId));
140 public function testMyJobs()
146 $this->uploadPermissionDao->shouldReceive(
'isAccessible')->withArgs(array(anything(),$groupId))
147 ->andReturnUsing(
function($upload,$group)
149 return ($upload==1 || $upload==2 || $upload==4);
151 $testOurJobs = $this->showJobsDao->myJobs(
true);
152 assertThat($testOurJobs[0], is(arrayContainingInAnyOrder($this->job_pks)));
153 $testMyJobs = $this->showJobsDao->myJobs(
false);
154 assertThat($testMyJobs, equalTo(array(array(1), 0)));
156 $this->
dbManager->queryOnce(
"UPDATE job SET job_queued=job_queued-INTERVAL '30 days' WHERE job_pk=1");
157 $this->
dbManager->prepare(__METHOD__.
'insert.perm_upload',
158 "INSERT INTO perm_upload (perm_upload_pk, perm, upload_fk, group_fk) VALUES ($1, $2, $3, $4)");
159 $testOutdatedJobs = $this->showJobsDao->myJobs(
true);
160 assertThat($testOutdatedJobs, equalTo(array(array(2), 0)));
163 public function testgetNumItemsPerSec()
166 $testFilesPerSec = $this->showJobsDao->getNumItemsPerSec(5*$numSecs, $numSecs);
167 assertThat($testFilesPerSec,is(greaterThan(1)));
169 $testFilesPerSec = $this->showJobsDao->getNumItemsPerSec(0.9*$numSecs, $numSecs);
170 assertThat($testFilesPerSec,is(lessThanOrEqualTo(1)));
173 public function testGetJobInfo()
175 $this->
dbManager->prepare($stmt =
'insert.jobqueue',
176 "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)"
177 .
"VALUES ($1, $2, $3, $4,$5, $6,$7,$8,$9,$10)");
181 $nomosTime = date(
'Y-m-d H:i:sO',$nowTime-$diffTime);
182 $uploadArrayQue = array(array(8, $jobId=1,
"nomos", 1,$nomosTime,
null ,
"Started", 0,
"localhost.5963", $itemNomos=147),
183 array(1, $jobId,
"ununpack", 1,
"2015-04-21 18:29:19.23825+05:30",
"2015-04-21 18:29:26.396562+05:30",
"Completed",1,
null,$itemCount=646 ));
184 foreach ($uploadArrayQue as $uploadEntry) {
188 $this->
dbManager->prepare($stmt =
'insert.uploadtree_a',
189 "INSERT INTO uploadtree_a (uploadtree_pk, parent, upload_fk, pfile_fk, ufile_mode, lft, rgt, ufile_name)"
190 .
"VALUES ($1, $2, $3, $4,$5, $6, $7, $8)");
191 $uploadTreeArray = array(array(123, 121, 1, 103, 32768, 542, 543,
"fckeditorcode_ie.js"),
192 array(121,120, 1, 0, 536888320, 537, 544,
"js"),
193 array(715,651, 2,607 ,33188 ,534 ,535 ,
"zconf.h.cmakein"),
194 array(915, 651, 2, 606 ,33188 ,532 ,533 ,
"zconf.h"),
196 foreach ($uploadTreeArray as $uploadEntry) {
200 $this->
dbManager->prepare($stmt =
'insert.jobdepends',
201 "INSERT INTO jobdepends (jdep_jq_fk, jdep_jq_depends_fk) VALUES ($1, $2 )");
202 $jqWithTwoDependencies = 8;
203 $jobDependsArray = array(array(2,1),
208 array($jqWithTwoDependencies,4),
209 array($jqWithTwoDependencies,4),
211 foreach ($jobDependsArray as $uploadEntry) {
215 $testMyJobInfo = $this->showJobsDao->getJobInfo($this->job_pks);
216 assertThat($testMyJobInfo,hasKey($jobId));
217 assertThat($testMyJobInfo[$jobId][
'jobqueue'][$jqWithTwoDependencies][
'depends'], is(arrayWithSize(2)));
219 $testFilesPerSec = 0.23;
220 $formattedEstimatedTime = $this->showJobsDao->getEstimatedTime($job_pk=1, $jq_Type=
"nomos", $testFilesPerSec);
221 assertThat($formattedEstimatedTime, matchesPattern (
'/\\d+:\\d{2}:\\d{2}/'));
222 $hourMinSec = explode(
':', $formattedEstimatedTime);
223 assertThat($hourMinSec[0]*3600+$hourMinSec[1]*60+$hourMinSec[2],
224 is(closeTo(($itemCount-$itemNomos)/$testFilesPerSec,0.5+$testFilesPerSec)));
226 $testGetEstimatedTime = $this->showJobsDao->getEstimatedTime($job_pk=1, $jq_Type, 0);
227 assertThat($testGetEstimatedTime, matchesPattern (
'/\\d+:\\d{2}:\\d{2}/'));
228 $hourMinSec = explode(
':', $testGetEstimatedTime);
229 $tolerance = 0.5+($itemCount-$itemNomos)/$itemNomos+(time()-$nowTime);
230 assertThat($hourMinSec[0]*3600+$hourMinSec[1]*60+$hourMinSec[2],
231 is(closeTo(($itemCount-$itemNomos)/$itemNomos*$diffTime,$tolerance)));
233 $fewFilesPerSec = 0.003;
234 $formattedLongTime = $this->showJobsDao->getEstimatedTime($job_pk=1, $jq_Type=
"nomos", $fewFilesPerSec);
235 assertThat($formattedLongTime, matchesPattern (
'/\\d+:\\d{2}:\\d{2}/'));
236 $hourMinSec = explode(
':', $formattedLongTime);
237 assertThat($hourMinSec[0]*3600+$hourMinSec[1]*60+$hourMinSec[2],
238 is(closeTo(($itemCount-$itemNomos)/$fewFilesPerSec,0.5+$fewFilesPerSec)));
241 public function testGetEstimatedTimeShouldNotDivideByZero()
243 $this->
dbManager->prepare($stmt =
'insert.jobqueue',
244 "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)"
245 .
"VALUES ($1, $2, $3, $4,$5, $6,$7,$8,$9,$10)");
249 $nomosTime = date(
'Y-m-d H:i:sO',$nowTime-$diffTime);
250 $uploadArrayQue = array(array(8, $jobId=1, $jqType=
"nomos", 1,$nomosTime,
null ,
"Started", 0,
"localhost.5963", $itemNomos=147),
251 array(1, $jobId,
"ununpack", 1,
"2015-04-21 18:29:19.23825+05:30",
"2015-04-21 18:29:26.396562+05:30",
"Completed",1,
null,$itemCount=646 ));
252 foreach ($uploadArrayQue as $uploadEntry) {
256 $showJobsDaoMock = M::mock(
'Fossology\\Lib\\Dao\\ShowJobsDao[getNumItemsPerSec]',array($this->
dbManager, $this->uploadDao));
257 $showJobsDaoMock->shouldReceive(
'getNumItemsPerSec')->andReturn(0);
259 $estimated = $showJobsDaoMock->getEstimatedTime($jobId, $jqType);
260 assertThat($estimated, equalTo(
'0:00:00'));
262 private function insertJobQue($jobQueueEndBits = 2)
264 $jobs = array(3=>2, 4=>3, 5=>5, 6=>8%6, 7=>13%6, 8=>21%6);
265 foreach ($jobs as $jobId => $jobUpload) {
266 $this->
dbManager->insertTableRow(
'job', array(
'job_pk' => $jobId,
'job_upload_fk' => $jobUpload));
268 $this->
dbManager->prepare($stmt =
'insert.jobqueue',
269 "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)"
270 .
"VALUES ($1, $2, $3, $4,$5, $6,$7,$8,$9,$10)");
274 $nomosTime = date(
'Y-m-d H:i:sO',$nowTime-$diffTime);
275 $uploadArrayQue = array(array(8, $jobId=1, $jqType=
"nomos", 1,$nomosTime,
null ,
"Started", 0,
"localhost.5963", $itemNomos=147),
276 array(1, $jobId,
"ununpack", 1,
"2015-04-21 18:29:19.23825+05:30",
"2015-04-21 18:29:26.396562+05:30",
"Completed",$jobQueueEndBits,
null,$itemCount=646 ));
277 foreach ($uploadArrayQue as $uploadEntry) {
281 public function testGetJobsForAll()
283 $this->insertJobQue();
284 $result = $this->showJobsDao->getJobsForAll();
285 $this->assertNotNull($result);
286 $this->assertEquals(array(array(
'job' =>
'nomos',
'jq_job_fk' =>
"1",
"upload_fk" =>
"1",
"status" =>
"running")),$result);
288 public function testGetItemsProcessedForDecider()
290 $this->insertJobQue();
291 $result = $this->showJobsDao->getItemsProcessedForDecider(
"nomos",1);
292 $expectedArray = array(
"147",
"1");
293 $this->assertNotNull($result);
294 $this->assertEquals($expectedArray,$result);
296 public function testGetJobStatusFalse()
298 $this->insertJobQue();
299 $jobStatus = $this->showJobsDao->getJobStatus(1);
300 $this->assertNotNull($jobStatus);
301 $this->assertFalse($jobStatus);
303 public function testGetJobStatusTrue()
305 $this->insertJobQue(4);
306 $jobStatus = $this->showJobsDao->getJobStatus(1);
307 $this->assertNotNull($jobStatus);
308 $this->assertTrue($jobStatus);
310 public function testGetDataASingleJob()
312 $this->insertJobQue();
313 $result = $this->showJobsDao->getDataForASingleJob(1);
314 $this->assertNotNull($result);
315 $expectedData = array(
318 'jq_type' =>
"ununpack",
320 'jq_starttime' => $result[
'jq_starttime'],
321 'jq_endtime' => $result[
'jq_endtime'],
322 'jq_endtext' =>
"Completed",
323 'jq_end_bits' =>
"2",
324 'jq_schedinfo' => NULL,
325 'jq_itemsprocessed' =>
"646",
327 'jq_runonpfile' => NULL,
329 'jq_cmd_args' => NULL,
331 'job_queued' => $result[
'job_queued'],
332 'job_priority' => NULL,
333 'job_email_notify' => NULL,
334 'job_name' =>
"FCKeditor_2.6.4.zip",
335 'job_upload_fk' =>
"1",
336 'job_folder_fk' => NULL,
337 'job_user_fk' =>
"1",
338 'job_group_fk' => NULL,
339 'elapsed' =>
"00:00:07.158312"
341 $this->assertEquals($expectedData,$result);
Contains the constants and helpers for authentication of user.
fo_dbManager * dbManager
fo_dbManager object