Skip to content

Commit

Permalink
Merge pull request fossology#2871 from andreas-menzl/andreas-menzl/fi…
Browse files Browse the repository at this point in the history
…x/empty-recent-job-pages

Fix(ui): Fixed wrong pagination for 'Show Jobs' page & missing upload accessible check

Reviewed-by: kaushlendra-pratap.singh@siemens.com
Tested-by: kaushlendra-pratap.singh@siemens.com
  • Loading branch information
shaheemazmalmmd authored Dec 10, 2024
2 parents 6046391 + 1d6aed3 commit ed40d0f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
27 changes: 17 additions & 10 deletions src/lib/php/Dao/ShowJobsDao.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ function __construct(DbManager $dbManager, UploadDao $uploadDao)
function uploads2Jobs($upload_pks, $page = 0)
{
$jobArray = array();

// only use the uploads the user / group has access to
$upload_pks = array_filter($upload_pks, function($upload_pk) {
return $upload_pk !== null && $this->uploadDao->isAccessible($upload_pk, Auth::getGroupId());
});

// get count of upload pks, return empty array if count equals 0
$jobCount = count($upload_pks);
if ($jobCount == 0) {
return $jobArray;
Expand Down Expand Up @@ -101,19 +108,12 @@ public function myJobs($allusers, $page = 0)
$allusers_str = ($allusers == 0) ? "job_user_fk='" . Auth::getUserId() .
"' and " : "";

$statementName = __METHOD__ . ".countJobs." . $allusers_str;
$sql = "SELECT count(*) AS cnt FROM job WHERE $allusers_str " .
"job_queued >= (now() - interval '" . $this->nhours . " hours');";

$countJobs = $this->dbManager->getSingleRow($sql, [], $statementName)['cnt'];
$totalPages = floor($countJobs / $this->maxJobsPerPage);

$statementName = __METHOD__ . "." . $allusers_str;
$this->dbManager->prepare($statementName,
"SELECT job_pk, job_upload_fk FROM job " . "WHERE $allusers_str " .
"job_queued >= (now() - interval '" . $this->nhours . " hours') " .
"ORDER BY job_queued DESC OFFSET $1 LIMIT " . $this->maxJobsPerPage);
$result = $this->dbManager->execute($statementName, [$offset]);
"ORDER BY job_queued DESC");
$result = $this->dbManager->execute($statementName);
while ($row = $this->dbManager->fetchArray($result)) {
if (! empty($row['job_upload_fk'])) {
$uploadIsAccessible = $this->uploadDao->isAccessible(
Expand All @@ -124,9 +124,16 @@ public function myJobs($allusers, $page = 0)
}
$jobArray[] = $row['job_pk'];
}

// calculate total pages for jobs accessible to current group
$totalPages = floor(count($jobArray) / $this->maxJobsPerPage);

// get jobs for current page only
$pageJobs = array_slice($jobArray, $offset, $this->maxJobsPerPage);

$this->dbManager->freeResult($result);

return array($jobArray, $totalPages);
return array($pageJobs, $totalPages);
} /* myJobs() */

/**
Expand Down
37 changes: 26 additions & 11 deletions src/lib/php/Dao/test/ShowJobsDaoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,38 +77,53 @@ protected function tearDown() : void

public function testUploads2Jobs()
{
$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==3 || $upload==4 || $upload==5);
});

$jobs = array(3=>2, 4=>3, 5=>5, 6=>8%6, 7=>13%6, 8=>21%6);
foreach ($jobs as $jobId => $jobUpload) {
$this->dbManager->insertTableRow('job', array('job_pk' => $jobId, 'job_upload_fk' => $jobUpload));
}
$uploadDao = M::mock('Fossology\Lib\Dao\UploadDao');
$showJobDao = new ShowJobsDao($this->dbManager,$uploadDao);
$jobsWithoutUpload = $showJobDao->uploads2Jobs(array());
$jobsWithoutUpload = $this->showJobsDao->uploads2Jobs(array());
assertThat($jobsWithoutUpload, is(emptyArray()));
$jobsWithUploadIdOne = $showJobDao->uploads2Jobs(array(1));
$jobsWithUploadIdOne = $this->showJobsDao->uploads2Jobs(array(1));
assertThat($jobsWithUploadIdOne, equalTo(array(array(1,7),0)));
$jobsAtAll = $showJobDao->uploads2Jobs(array(1,2,3,4,5));
$jobsAtAll = $this->showJobsDao->uploads2Jobs(array(1,2,3,4,5));
assertThat($jobsAtAll, equalTo(array(array(1,7, 2,3,6, 4,8, 5),0)));
$jobsWithUploadFour = $showJobDao->uploads2Jobs(array(4));
$jobsWithUploadFour = $this->showJobsDao->uploads2Jobs(array(4));
assertThat($jobsWithUploadFour[0], is(emptyArray()));
}

public function testUploads2JobsPaged()
{
$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 range(1, 17);
});

$jobs = array_combine(range(3,13),range(3,13));
foreach ($jobs as $jobId => $jobUpload) {
$this->dbManager->insertTableRow('job', array('job_pk' => $jobId, 'job_upload_fk' => $jobUpload));
}
$uploadDao = M::mock('Fossology\Lib\Dao\UploadDao');
$showJobDao = new ShowJobsDao($this->dbManager,$uploadDao);

$jobsPage1 = $showJobDao->uploads2Jobs(range(1,17),0);
$jobsPage1 = $this->showJobsDao->uploads2Jobs(range(1,17),0);
assertThat($jobsPage1[0], arrayWithSize(10));
assertThat($jobsPage1[1], is(1));
$jobsPage2 = $showJobDao->uploads2Jobs(array_combine(range(10,16),range(11,17)),1);
$jobsPage2 = $this->showJobsDao->uploads2Jobs(array_combine(range(10,16),range(11,17)),1);
assertThat($jobsPage2[0], arrayWithSize(3));
assertThat($jobsPage2[1], is(0));
$jobsPage3 = $showJobDao->uploads2Jobs(array(),2);
$jobsPage3 = $this->showJobsDao->uploads2Jobs(array(),2);
assertThat($jobsPage3, arrayWithSize(0));
}

Expand Down

0 comments on commit ed40d0f

Please sign in to comment.