Some automatic tests for status/percent changes

This commit is contained in:
nathangray 2017-02-02 16:53:59 -07:00
parent eae8821b97
commit 562236c3ae
2 changed files with 264 additions and 0 deletions

181
infolog/test/StatusTest.php Normal file
View File

@ -0,0 +1,181 @@
<?php
/**
* Test the status / completion combinations on an infolog entry
*
* Some statuses changes will also change the percent completed value.
* For example, closed statuses will set completed to 100%, subsequently changing
* to an open status will change completed to less than 100%. Changing to new
* (not-started) will set completion to 0%.
*
* The list of status and percentage changes is stored in a JSON file
*
* @link http://www.egroupware.org
* @author Nathan Gray
* @package infolog
* @copyright (c) 2017 by Nathan Gray
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
*/
namespace EGroupware\Infolog;
require_once realpath(__DIR__.'/../../api/src/test/AppTest.php'); // Application test base
use Egroupware\Api;
class StatusTest extends \EGroupware\Api\AppTest
{
private static $map_file = __DIR__.'/status_map.json';
protected $bo;
// Infolog under test
protected $info_id = null;
/**
* Create a custom status we can use to test
*/
public static function setUpBeforeClass()
{
parent::setUpBeforeClass();
// Create custom status
$bo = new \infolog_bo();
$bo->status['task']['custom'] = 'custom';
Api\Config::save_value('status',$bo->status,'infolog');
}
public static function tearDownAfterClass()
{
// Remove custom status
$bo = new \infolog_bo();
unset($bo->status['task']['custom']);
Api\Config::save_value('status',$bo->status,'infolog');
}
public function setUp()
{
$this->bo = new \infolog_bo();
$this->mockTracking($this->bo, 'infolog_tracking');
}
public function tearDown()
{
$this->bo = null;
}
/**
* Step through the map and check each one
*/
public function testStatusChange()
{
$json = file_get_contents(realpath(static::$map_file));
// Strip the comments out of the json file, they're not valid
$json = preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#", '', $json);
$map = json_decode($json, true);
$this->assertNotEquals(0, count($map), 'Could not read status map ' . static::$map_file);
foreach($map as $test => $map)
{
$this->checkOne($map['from'], $map['to'], $map['expected']);
}
}
/**
* Check a change
*
* @param Array $from
* @param Array $to
* @param Array $expected
*/
protected function checkOne($from, $to, $expected)
{
$info = $this->getTestInfolog($from);
// Skipping notifications - save initial state
$this->info_id = $this->bo->write($info, true, true, true, true);
foreach($to as $field => $value)
{
$info["info_{$field}"] = $value;
}
// Skipping notifications
$this->bo->write($info, true, true, true, true);
// Read it back to check
$saved = $this->bo->read($this->info_id);
$test_name = $this->getTestName($from, $to);
foreach($expected as $field => $value)
{
$this->assertEquals($value, $saved["info_{$field}"],
"$test_name failed on '$field' field");
}
// Remove infolog under test
if($this->info_id)
{
$this->bo->delete($this->info_id, False, False, True);
}
}
/**
* Get a text representation of the change so we can tell which one went
* wrong
*
* @param Array $from
* @param Array $to
* @return String
*/
protected function getTestName($from, $to)
{
$name = array();
foreach($from as $field => $value)
{
$name[] = $field . ': ' . $from[$field] . (array_key_exists($field, $to) ? ' => ' . $to[$field] : '');
}
return implode(', ', $name);
}
/**
* Set up a basic infolog entry for testing with the specified fields
* set.
*
* @param Array $from Fields to be set for initial conditions
* @return Array
*/
protected function getTestInfolog($from)
{
$info = array(
'info_subject' => 'Test Infolog Entry for ' . $this->getName()
);
foreach($from as $field => $value)
{
$info["info_{$field}"] = $value;
}
return $info;
}
/**
* Check status changes with custom status
*/
public function testCustomStatus()
{
$this->checkOne(
array('status' => 'custom', 'percent' => 10),
array('status' => 'ongoing'),
array('status' => 'ongoing', 'percent' => 10)
);
}
}

View File

@ -0,0 +1,83 @@
/* Map of status / percentage changes and their expected result */
[
{
"from": {"status": "not-started", "percent": 0},
"to": {"status": "ongoing"},
"expected": {"status": "ongoing", "percent": 10}
},
{
"from": {"status": "ongoing", "percent": 10},
"to": {"status": "not-started"},
"expected": {"status": "not-started", "percent": 0}
},
{
"from": {"status": "ongoing", "percent": 10},
"to": {"status": "done"},
"expected": {"status": "done", "percent": 100}
},
{
"from": {"status": "ongoing", "percent": 10},
"to": {"status": "billed"},
"expected": {"status": "billed", "percent": 100}
},
{
"from": {"status": "ongoing", "percent": 10},
"to": {"status": "cancelled"},
"expected": {"status": "cancelled", "percent": 10}
},
{
"from": {"status": "ongoing", "percent": 10},
"to": {"status": "custom"},
"expected": {"status": "custom", "percent": 10}
},
{
"from": {"status": "not-started", "percent": 0},
"to": {"status": "custom"},
// Custom status does not change percent when selected
"expected": {"status": "custom", "percent": 0}
},
{
"from": {"status": "custom", "percent": 10},
"to": {"status": "done"},
"expected": {"status": "done", "percent": 100}
},
{
"from": {"status": "ongoing", "percent": 20},
"to": {"status": "archived"},
// Note: no percent change, though it is 'closed'
"expected": {"status": "archived", "percent": 20}
},
{
"from": {"status": "ongoing", "percent": 10},
"to": {"status": "totally_invalid"},
// Not sure this is right. Should we accept invalid status?
"expected": {"status": "totally_invalid", "percent": 10}
},
{
"from": {"status": "done", "percent": 100},
"to": {"status": "totally_invalid"},
"expected": {"status": "done", "percent": 100}
},
{
"from": {"status": "ongoing", "percent": 10},
"to": {"percent": 20},
"expected": {"status": "ongoing", "percent": 20}
},
// @todo These ones differ between JS on client and BO
// Status takes priority over percent?
{
"from": {"status": "ongoing", "percent": 10},
"to": {"percent": 0},
"expected": {"status": "ongoing", "percent": 10}
},
{
"from": {"status": "ongoing", "percent": 10},
"to": {"percent": 100},
"expected": {"status": "ongoing", "percent": 10}
},
{
"from": {"status": "not-started", "percent": 0},
"to": {"percent": 10},
"expected": {"status": "not-started", "percent": 0}
}
]