mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-21 01:28:47 +02:00
N°6408 - CRUD : rework on DBUpdate reentrancy
This commit is contained in:
@@ -20,20 +20,17 @@ class ApplicationObjectExtensionTest extends \Combodo\iTop\Test\UnitTest\ItopDat
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
require_once 'iApplicationObjectExtension/ObjectModifyExtension.php';
|
||||
|
||||
// Add ObjectModifyExtension to the plugin list
|
||||
$this->InvokeNonPublicStaticMethod(MetaModel::class, 'InitExtensions', []);
|
||||
// Instantiate the new object
|
||||
$this->InvokeNonPublicStaticMethod(PluginManager::class, 'ResetPlugins', []);
|
||||
ObjectModifyExtension::SetCallBack([ApplicationObjectExtensionTest::class, 'IncrementCallCount']);
|
||||
require_once 'iApplicationObjectExtension/MockApplicationObjectExtensionForTest.php';
|
||||
$this->ResetApplicationObjectExtensions();
|
||||
// Count all the calls to this object
|
||||
MockApplicationObjectExtensionForTest::SetCallBack([ApplicationObjectExtensionTest::class, 'IncrementCallCount']);
|
||||
}
|
||||
|
||||
public function tearDown(): void
|
||||
{
|
||||
ObjectModifyExtension::SetModifications('Person', 'name', 0);
|
||||
ObjectModifyExtension::SetAlwaysChanged(false);
|
||||
ObjectModifyExtension::SetCallBack(null);
|
||||
MockApplicationObjectExtensionForTest::SetModifications('Person', 'name', 0);
|
||||
MockApplicationObjectExtensionForTest::SetCallBack(null);
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
@@ -54,10 +51,11 @@ class ApplicationObjectExtensionTest extends \Combodo\iTop\Test\UnitTest\ItopDat
|
||||
// Check that extension is called
|
||||
$oPerson = $this->CreatePerson(1);
|
||||
$oPerson->Set('first_name', 'testUpdateReentranceProtection');
|
||||
ObjectModifyExtension::SetModifications('Person', 'name', 1);
|
||||
MockApplicationObjectExtensionForTest::SetModifications('Person', 'name', 1);
|
||||
self::ResetCallCount();
|
||||
$oPerson->DBUpdate();
|
||||
$this->assertEquals(1, self::$iCalls);
|
||||
// Called twice, the first call will provoke the DBUpdate and call again the object extension
|
||||
$this->assertEquals(2, self::$iCalls);
|
||||
}
|
||||
|
||||
public function testUpdateReentranceProtection()
|
||||
@@ -67,22 +65,44 @@ class ApplicationObjectExtensionTest extends \Combodo\iTop\Test\UnitTest\ItopDat
|
||||
// Check that loop limit is 10
|
||||
$i = 15;
|
||||
self::ResetCallCount();
|
||||
ObjectModifyExtension::SetModifications('Person', 'name', $i);
|
||||
MockApplicationObjectExtensionForTest::SetModifications('Person', 'name', $i);
|
||||
$oPerson->Set('first_name', 'testUpdateReentranceProtection');
|
||||
$oPerson->DBUpdate();
|
||||
$this->assertEquals(10, self::$iCalls);
|
||||
}
|
||||
|
||||
public function testModificationsLost()
|
||||
public function testModificationsOnUpdate()
|
||||
{
|
||||
self::ResetCallCount();
|
||||
$oPerson = $this->CreatePerson(1);
|
||||
$oPerson->Set('first_name', 'testUpdateReentranceProtection');
|
||||
|
||||
ObjectModifyExtension::SetModifications('Person', 'name', 1);
|
||||
ObjectModifyExtension::SetAlwaysChanged(true);
|
||||
self::ResetCallCount();
|
||||
MockApplicationObjectExtensionForTest::SetModifications('Person', 'name', 1);
|
||||
$oPerson->DBUpdate();
|
||||
$this->assertEquals(1, self::$iCalls);
|
||||
$this->assertEquals(2, self::$iCalls);
|
||||
}
|
||||
|
||||
public function testModificationsOnInsert()
|
||||
{
|
||||
self::ResetCallCount();
|
||||
MockApplicationObjectExtensionForTest::SetModifications('Person', 'name', 1);
|
||||
$oPerson = $this->CreatePerson(1);
|
||||
$this->assertEquals(2, self::$iCalls);
|
||||
}
|
||||
|
||||
|
||||
public function testModificationsOnInsertWith2Extensions()
|
||||
{
|
||||
self::ResetCallCount();
|
||||
require_once 'iApplicationObjectExtension/MockApplicationObjectExtensionForTest2.php';
|
||||
$this->ResetApplicationObjectExtensions();
|
||||
// Count all the calls to this object
|
||||
MockApplicationObjectExtensionForTest2::SetCallBack([ApplicationObjectExtensionTest::class, 'IncrementCallCount']);
|
||||
|
||||
MockApplicationObjectExtensionForTest::SetModifications('Person', 'name', 2);
|
||||
MockApplicationObjectExtensionForTest2::SetModifications('Person', 'first_name', 2);
|
||||
$oPerson = $this->CreatePerson(1);
|
||||
$this->assertEquals(6, self::$iCalls);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2023 Combodo SARL
|
||||
* @license http://opensource.org/licenses/AGPL-3.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Test object for AbstractApplicationObjectExtension API
|
||||
*/
|
||||
class MockApplicationObjectExtensionForTest extends AbstractApplicationObjectExtension
|
||||
{
|
||||
protected static $iCountModify;
|
||||
protected static $sClass;
|
||||
protected static $sAttCodeToModify;
|
||||
protected static $callBack;
|
||||
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public static function SetCallBack($callBack)
|
||||
{
|
||||
static::$callBack = $callBack;
|
||||
}
|
||||
|
||||
public static function SetModifications($sClass, $sAttCodeToModify, $iCountModify)
|
||||
{
|
||||
static::$sClass = $sClass;
|
||||
static::$sAttCodeToModify = $sAttCodeToModify;
|
||||
if (!MetaModel::IsValidClass($sClass) || !MetaModel::IsValidAttCode($sClass, $sAttCodeToModify)) {
|
||||
throw new Exception("Invalid class $sClass or attcode $sAttCodeToModify");
|
||||
}
|
||||
static::$iCountModify = $iCountModify;
|
||||
}
|
||||
|
||||
public function OnDBUpdate($oObject, $oChange = null)
|
||||
{
|
||||
if (get_class($oObject) !== static::$sClass) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!is_null(static::$callBack)) {
|
||||
call_user_func(static::$callBack, 'OnDBUpdate');
|
||||
}
|
||||
|
||||
$aPreviousValues = $oObject->ListPreviousValuesForUpdatedAttributes();
|
||||
$sPreviousValues = print_r($aPreviousValues, true);
|
||||
|
||||
IssueLog::Info(__METHOD__." received previous values:\n$sPreviousValues");
|
||||
|
||||
if (static::$iCountModify > 0) {
|
||||
static::$iCountModify--;
|
||||
$oObject->Set(static::$sAttCodeToModify, 'Value_'.rand());
|
||||
$oObject->DBUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
public function OnDBInsert($oObject, $oChange = null)
|
||||
{
|
||||
if (get_class($oObject) !== static::$sClass) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!is_null(static::$callBack)) {
|
||||
call_user_func(static::$callBack, 'OnDBInsert');
|
||||
}
|
||||
|
||||
if (static::$iCountModify > 0) {
|
||||
static::$iCountModify--;
|
||||
$oObject->Set(static::$sAttCodeToModify, 'Value_'.rand());
|
||||
$oObject->DBUpdate();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2023 Combodo SARL
|
||||
* @license http://opensource.org/licenses/AGPL-3.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Test object for AbstractApplicationObjectExtension API
|
||||
*/
|
||||
class MockApplicationObjectExtensionForTest2 extends AbstractApplicationObjectExtension
|
||||
{
|
||||
protected static $iCountModify;
|
||||
protected static $sClass;
|
||||
protected static $sAttCodeToModify;
|
||||
protected static $callBack;
|
||||
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public static function SetCallBack($callBack)
|
||||
{
|
||||
static::$callBack = $callBack;
|
||||
}
|
||||
|
||||
public static function SetModifications($sClass, $sAttCodeToModify, $iCountModify)
|
||||
{
|
||||
static::$sClass = $sClass;
|
||||
static::$sAttCodeToModify = $sAttCodeToModify;
|
||||
if (!MetaModel::IsValidClass($sClass) || !MetaModel::IsValidAttCode($sClass, $sAttCodeToModify)) {
|
||||
throw new Exception("Invalid class $sClass or attcode $sAttCodeToModify");
|
||||
}
|
||||
static::$iCountModify = $iCountModify;
|
||||
}
|
||||
|
||||
public function OnDBUpdate($oObject, $oChange = null)
|
||||
{
|
||||
if (get_class($oObject) !== static::$sClass) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!is_null(static::$callBack)) {
|
||||
call_user_func(static::$callBack, 'OnDBUpdate');
|
||||
}
|
||||
|
||||
$aPreviousValues = $oObject->ListPreviousValuesForUpdatedAttributes();
|
||||
$sPreviousValues = print_r($aPreviousValues, true);
|
||||
|
||||
IssueLog::Info(__METHOD__." received previous values:\n$sPreviousValues");
|
||||
|
||||
if (static::$iCountModify > 0) {
|
||||
static::$iCountModify--;
|
||||
$oObject->Set(static::$sAttCodeToModify, 'Value_'.rand());
|
||||
$oObject->DBUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
public function OnDBInsert($oObject, $oChange = null)
|
||||
{
|
||||
if (get_class($oObject) !== static::$sClass) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!is_null(static::$callBack)) {
|
||||
call_user_func(static::$callBack, 'OnDBInsert');
|
||||
}
|
||||
|
||||
if (static::$iCountModify > 0) {
|
||||
static::$iCountModify--;
|
||||
$oObject->Set(static::$sAttCodeToModify, 'Value_'.rand());
|
||||
$oObject->DBUpdate();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2023 Combodo SARL
|
||||
* @license http://opensource.org/licenses/AGPL-3.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Test object for AbstractApplicationObjectExtension API
|
||||
*/
|
||||
class ObjectModifyExtension extends AbstractApplicationObjectExtension
|
||||
{
|
||||
private static $iCountModify;
|
||||
private static $bAlwaysChanged;
|
||||
private static $sClass;
|
||||
private static $sAttCodeToModify;
|
||||
private static $callBack;
|
||||
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public static function SetCallBack($callBack)
|
||||
{
|
||||
self::$callBack = $callBack;
|
||||
}
|
||||
|
||||
public static function SetModifications($sClass, $sAttCodeToModify, $iCountModify)
|
||||
{
|
||||
self::$sClass = $sClass;
|
||||
self::$sAttCodeToModify = $sAttCodeToModify;
|
||||
if (!MetaModel::IsValidClass($sClass) || !MetaModel::IsValidAttCode($sClass, $sAttCodeToModify)) {
|
||||
throw new Exception("Invalid class $sClass or attcode $sAttCodeToModify");
|
||||
}
|
||||
self::$iCountModify = $iCountModify;
|
||||
}
|
||||
|
||||
public static function SetAlwaysChanged($bAlwaysChanged)
|
||||
{
|
||||
self::$bAlwaysChanged = $bAlwaysChanged;
|
||||
}
|
||||
|
||||
public function OnDBUpdate($oObject, $oChange = null)
|
||||
{
|
||||
if (get_class($oObject) !== self::$sClass) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (self::$iCountModify > 0) {
|
||||
if (!empty($oObject->ListPreviousValuesForUpdatedAttributes())) {
|
||||
if (!is_null(self::$callBack)) {
|
||||
call_user_func(self::$callBack, 'OnDBUpdate');
|
||||
}
|
||||
self::$iCountModify--;
|
||||
$oObject->Set(self::$sAttCodeToModify, 'Value_'.rand());
|
||||
$oObject->DBUpdate();
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function OnIsModified($oObject)
|
||||
{
|
||||
return self::$bAlwaysChanged;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user