silverstripe-externallinks/src/Model/BrokenExternalPageTrackStatus.php
Dylan Wagstaff 98dac9b314 Update unit tests to be rid of Phockito
This odd dependency is causing inclusion issues to do with namespacing and
the ability to work well with the framework (according to the dependency's
readme). The usage of the tool in this case adds no value, as it's
performing purely stub type work, rather than mock work. So instead we can
use an actual stub (simply return test values depending on input) and have
everything work well.

After removing the Phockito dependency it was found that the tests that
would skip when it isn't present were actually in a failing state. So they
had to be repaired too, mostly through the use of fixing the poor code
that was causing them to fail (as opposed to being bad tests). This is
both configuration and proceedural work.
2017-11-27 15:14:16 +13:00

154 lines
3.6 KiB
PHP

<?php
namespace SilverStripe\ExternalLinks\Model;
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrack;
use SilverStripe\ExternalLinks\Model\BrokenExternalLink;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Versioned\Versioned;
use SilverStripe\ORM\DataObject;
/**
* Represents the status of a track run
*
* @method DataList TrackedPages()
* @method DataList BrokenLinks()
* @property int $TotalPages Get total pages count
* @property int $CompletedPages Get completed pages count
*/
class BrokenExternalPageTrackStatus extends DataObject
{
private static $table_name = 'BrokenExternalPageTrackStatus';
private static $db = array(
'Status' => 'Enum("Completed, Running", "Running")',
'JobInfo' => 'Varchar(255)'
);
private static $has_many = array(
'TrackedPages' => BrokenExternalPageTrack::class,
'BrokenLinks' => BrokenExternalLink::class
);
/**
* Get the latest track status
*
* @return BrokenExternalPageTrackStatus
*/
public static function get_latest()
{
return self::get()
->sort('ID', 'DESC')
->first();
}
/**
* Gets the list of Pages yet to be checked
*
* @return DataList
*/
public function getIncompletePageList()
{
$pageIDs = $this
->getIncompleteTracks()
->column('PageID');
if ($pageIDs) {
return Versioned::get_by_stage(SiteTree::class, 'Stage')
->byIDs($pageIDs);
}
}
/**
* Get the list of incomplete BrokenExternalPageTrack
*
* @return DataList
*/
public function getIncompleteTracks()
{
return $this
->TrackedPages()
->filter('Processed', 0);
}
/**
* Get total pages count
*
* @return int
*/
public function getTotalPages()
{
return $this->TrackedPages()->count();
}
/**
* Get completed pages count
*
* @return int
*/
public function getCompletedPages()
{
return $this
->TrackedPages()
->filter('Processed', 1)
->count();
}
/**
* Returns the latest run, or otherwise creates a new one
*
* @return BrokenExternalPageTrackStatus
*/
public static function get_or_create()
{
// Check the current status
$status = self::get_latest();
if ($status && $status->Status == 'Running') {
$status->updateStatus();
return $status;
}
return self::create_status();
}
/**
* Create and prepare a new status
*
* @return BrokenExternalPageTrackStatus
*/
public static function create_status()
{
// If the script is to be started create a new status
$status = self::create();
$status->updateJobInfo('Creating new tracking object');
// Setup all pages to test
$pageIDs = Versioned::get_by_stage(SiteTree::class, 'Stage')
->column('ID');
foreach ($pageIDs as $pageID) {
$trackPage = BrokenExternalPageTrack::create();
$trackPage->PageID = $pageID;
$trackPage->StatusID = $status->ID;
$trackPage->write();
}
return $status;
}
public function updateJobInfo($message)
{
$this->JobInfo = $message;
$this->write();
}
/**
* Self check status
*/
public function updateStatus()
{
if ($this->CompletedPages == $this->TotalPages) {
$this->Status = 'Completed';
$this->updateJobInfo('Setting to completed');
}
}
}