Multiple Status Changes in a single thread?


(David Schoen) #1

I have been trying to get some Unit Tests with PHPUnit up and running for code we are writing that integrates with Matrix. The only way I can see to do some of the Unit Tests basically requires me to be able to change the status of an asset multple times within a single thread. Without being able to do this I don't think I can write useful Unit Tests using PHPUnit.


The problem I've hit is that after the status of an asset has been changed I seem to need to exit the thread before I can reliably deal with that asset again.



I simplified the code down to this:

[codebox]<?php

require_once '/websites/cms/unit-test/core/include/init.inc';



// asset we can play with

$assetid = 49;



// login as root user

$root_user = &$GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user');

if (!$GLOBALS['SQ_SYSTEM']->setCurrentUser($root_user)) {

trigger_error("Failed logging in as root user\n", E_USER_ERROR);

}



/*

Basically what I'm doing here is:

print starting status

Change status to live, reguardless of current status

print current status

Ask asset manager to forget asset

print current status

Change status to Under Construction, reguardless of current status

print current status

Ask asset manager to forget asset

print current status

*/



$asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);

$status = $asset->getStatus();

echo "Status at start: ".$status->getDescription()."\n";



if (!$asset->processStatusChange(SQ_STATUS_LIVE)) {

trigger_error('Failed to set status', E_USER_ERROR);

}



$status = $asset->getStatus();

echo "Status after change to live: ".$status->getDescription()."\n";



$GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);

unset($asset);



$asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);

$status = $asset->getStatus();

echo "Status after forget: ".$status->getDescription()."\n";



if (!$asset->processStatusChange(SQ_STATUS_UNDER_CONSTRUCTION)) {

trigger_error('Failed to set status', E_USER_ERROR);

}



$status = $asset->getStatus();

echo "Status after change to UC: ".$status->getDescription()."\n";



$GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);

unset($asset);



$asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);

$status = $asset->getStatus();

echo "Status after forget: ".$status->getDescription()."\n";



?>[/codebox]



Which results in:

[codebox]matrix@vm ~$ php debug.php

Status at start: Under Construction

Status after change to live: Live

Status after forget: Under Construction

Status after change to UC: Under Construction

Status after forget: Under Construction

matrix@vm ~$ php debug.php

Status at start: Live

Status after change to live: Live

Status after forget: Live

Status after change to UC: Under Construction

Status after forget: Live

matrix@vm ~$ php debug.php

Status at start: Under Construction

Status after change to live: Live

Status after forget: Under Construction

Status after change to UC: Under Construction

Status after forget: Under Construction[/codebox]



Has anyone come across this before?

Does anyone have any work arounds?

Have I missed something obvious?

Is anyone writing/using Unit Tests with Matrix already?



I'm devloping against 3.16.6 SSV at the moment.


(Mark Brydon) #2

[quote]

    // …
$asset = $GLOBALS[‘SQ_SYSTEM’]->am->getAsset($assetid);
// …
[/quote]

Please note that the call to getAsset() in our PHP 4 releases (ie; up to 3.16.15) returns a reference and should therefore be represented in code as follows:
    $asset =& $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);

This explicit declaration is not required in PHP 5 (ie; from 3.18.x) as PHP passes references internally.


(Huan Nguyen) #3

You should change the status of the asset by running a hipo job called hipo_job_edit_status, this is how you can run it

    	
    	$assetid	= 'XXXX';
    	$vars	   = Array(
    					'assetid'	   => $assetid,
    					'new_status'	=> SQ_STATUS_UNDER_CONSTRUCTION,
    					'process_dependents' => TRUE,
    				  );
    
    
    	$hh =& $GLOBALS['SQ_SYSTEM']->getHipoHerder();
    	$hh->freestyleHipo('hipo_job_edit_status', $vars);
    
    	$asset  =& $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
    	bam($asset->getStatus());
    
    
    	$vars	   = Array(
    					'assetid'	   => $assetid,
    					'new_status'	=> SQ_STATUS_LIVE,
    					'process_dependents' => TRUE,
    				  );
    
    	$hh =& $GLOBALS['SQ_SYSTEM']->getHipoHerder();
    	$hh->freestyleHipo('hipo_job_edit_status', $vars);
    
    	bam($asset->getStatus());


This piece of code work perfectly and does change the status of the asset twice

(David Schoen) #4

That works, thanks :slight_smile: