dolibarr/test/phpunit/MoTest.php

268 lines
7.8 KiB
PHP
Raw Permalink Normal View History

NEW: Add phpunit test for Mo class (#39535) * NEW: Add phpunit test for Mo class Add a unit-level test for the Mo (manufacturing order) class, which had no direct test coverage yet (RestAPIMoTest.php only exercises the REST API layer over HTTP, and its most interesting part - produce/consume - is entirely commented out there). Covers create() (including the automatic "to produce" line it creates for the finished product), fetch, update, the draft -> validated -> canceled -> validated status workflow (validate/cancel/reopen, including the provisional ref being replaced on validate()), and delete. Uses a freshly created specimen Product for fk_product rather than a random real catalog product: Mo::create() rejects kit/BOM products unless ALLOW_USE_KITS_INTO_BOM_AND_MO is set, and a specimen product is guaranteed not to be one. The module is auto-activated in setUpBeforeClass() if not already enabled, following the same pattern as the other recently added tests (activation is real and persists after the test run, it is not undone by the rollback in tearDownAfterClass - see comment in setUpBeforeClass() for why). * FIX MoTest crash in the full test suite (stale $db) Same class of bug as StockTransferTest (see that commit for the full analysis): modMrp depends on modBom, which itself depends on modProduct, whose constructor queries the DB via Societe::useNPR(). If $db is stale/closed when this class's setUpBeforeClass() runs in the full suite (all classes in one continuous process), activating modMrp crashes on the dead connection the same way. Reconnect $db (and refresh $mysoc->db/$user->db, which stashed their own reference to the old connection at bootstrap) before calling activateModule(), same pattern as StockTransferTest. Verified the same way: closing $db and calling MoTest::setUpBeforeClass() directly reproduces the crash without this fix and is resolved with it. * FIX MoTest: resync $mysoc/$user->db unconditionally Same follow-up fix as StockTransferTest (see that commit for the full analysis): the previous defensive-reconnect only refreshed $mysoc->db/$user->db inside the branch where the global $db itself was detected stale. $mysoc/$user can diverge from a healthy $db independently (they stash their own ->db reference at bootstrap), so resync them unconditionally instead. Verified the same way: closing the original $db but reconnecting only the global $db variable (leaving $mysoc->db pointing at the closed one) reproduces the crash with the old code and is resolved with this fix. * NEW Centralize the stale-$db reconnect logic in CommonClassTest Same follow-up as StockTransferTest (#39542): the defensive reconnect-and-resync-$mysoc/$user logic was duplicated identically across 3 test classes. Extract it into a shared CommonClassTest::ensureDbIsConnected() helper so future test classes that activate a module depending on modProduct don't need to reimplement it, and so the fix can't drift between test files. MoTest::setUpBeforeClass() now just calls self::ensureDbIsConnected() before activateModule(). Re-verified the same way as before: closing the original $db but reconnecting only the global $db variable (leaving $mysoc->db pointing at the closed one) still reproduces the crash without this fix and is resolved with it. * Remove setUpBeforeClass from MoTest Removed setUpBeforeClass method to simplify test setup. * Update CommonClassTest.class.php --------- Co-authored-by: Laurent Destailleur <eldy@destailleur.fr>
2026-08-19 00:28:13 +00:00
<?php
/* Copyright (C) 2026 Frédéric France <frederic.france@free.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* or see https://www.gnu.org/
*/
/**
* \file test/phpunit/MoTest.php
* \ingroup test
* \brief PHPUnit test
* \remarks To run this script as CLI: phpunit filename.php
*/
global $conf,$user,$langs,$db;
require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
require_once dirname(__FILE__).'/../../htdocs/mrp/class/mo.class.php';
require_once dirname(__FILE__).'/../../htdocs/product/class/product.class.php';
require_once dirname(__FILE__).'/CommonClassTest.class.php';
if (empty($user->id)) {
print "Load permissions for admin user nb 1\n";
$user->fetch(1);
$user->loadRights();
}
$conf->global->MAIN_DISABLE_ALL_MAILS = 1;
/**
* Class for PHPUnit tests
*
* @backupGlobals disabled
* @backupStaticAttributes enabled
* @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
*/
class MoTest extends CommonClassTest
{
/**
* testMoCreate
*
* Mo::create() needs a real product (fk_product): a random real catalog product cannot be used
* here, a kit/BOM product would be rejected by create() unless ALLOW_USE_KITS_INTO_BOM_AND_MO is
* set (see Mo::create()) - use a freshly created, plain (non-kit) specimen product instead.
*
* @return int
*/
public function testMoCreate()
{
global $conf,$user,$langs,$db;
$conf = $this->savconf;
$user = $this->savuser;
$langs = $this->savlangs;
$db = $this->savdb;
$product = new Product($db);
$product->initAsSpecimen();
$productid = $product->create($user);
$this->assertGreaterThan(0, $productid, $product->errorsToString());
$localobject = new Mo($db);
$localobject->initAsSpecimen();
// initAsSpecimen() sets a fixed ref ('ABCD1234'), but a real Mo is created with the '(PROV)'
// placeholder so createCommon() assigns it a real provisional ref ("(PROVid)") - use that here
// so the ref-renumbering on validate() tested below reflects real usage.
$localobject->ref = '(PROV)';
$localobject->fk_product = $productid;
$localobject->qty = 5;
$result = $localobject->create($user);
$this->assertGreaterThan(0, $result, $localobject->errorsToString());
print __METHOD__." result=".$result." fk_product=".$productid."\n";
// create() must have auto-created the "to produce" line for the finished product itself
// (no BOM is set here, so there is nothing to consume)
$localobject->fetch($result);
$toproduce = $localobject->fetchLinesLinked('toproduce');
$this->assertCount(1, $toproduce);
$this->assertEquals($productid, $toproduce[0]['fk_product']);
$this->assertEqualsWithDelta(5.0, (float) $toproduce[0]['qty'], 0.00001);
$this->assertCount(0, $localobject->fetchLinesLinked('toconsume'));
return $result;
}
/**
* testMoFetch
*
* @param int $id Id of object
* @return Mo
*
* @depends testMoCreate
* The depends says test is run only if previous is ok
*/
public function testMoFetch($id)
{
global $conf,$user,$langs,$db;
$conf = $this->savconf;
$user = $this->savuser;
$langs = $this->savlangs;
$db = $this->savdb;
$localobject = new Mo($db);
$result = $localobject->fetch($id);
$this->assertGreaterThan(0, $result, $localobject->errorsToString());
print __METHOD__." id=".$id." result=".$result."\n";
$this->assertEqualsWithDelta(5.0, (float) $localobject->qty, 0.00001);
$this->assertEquals(Mo::STATUS_DRAFT, $localobject->status);
$this->assertMatchesRegularExpression('/^\(?PROV/i', (string) $localobject->ref, 'A not yet validated Mo must have a provisional ref');
return $localobject;
}
/**
* testMoUpdate
*
* @param Mo $localobject Mo
* @return Mo
*
* @depends testMoFetch
* The depends says test is run only if previous is ok
*/
public function testMoUpdate($localobject)
{
global $conf,$user,$langs,$db;
$conf = $this->savconf;
$user = $this->savuser;
$langs = $this->savlangs;
$db = $this->savdb;
$localobject->label = 'Updated label after update';
$localobject->note_private = 'New note private after update';
$result = $localobject->update($user);
$this->assertGreaterThan(0, $result, $localobject->errorsToString());
print __METHOD__." id=".$localobject->id." result=".$result."\n";
$localobject->fetch($localobject->id);
$this->assertSame('Updated label after update', $localobject->label);
$this->assertSame('New note private after update', $localobject->note_private);
return $localobject;
}
/**
* testMoValidate
*
* @param Mo $localobject Mo
* @return Mo
*
* @depends testMoUpdate
* The depends says test is run only if previous is ok
*/
public function testMoValidate($localobject)
{
global $conf,$user,$langs,$db;
$conf = $this->savconf;
$user = $this->savuser;
$langs = $this->savlangs;
$db = $this->savdb;
$oldref = $localobject->ref;
$result = $localobject->validate($user);
$this->assertEquals(1, $result, $localobject->errorsToString());
print __METHOD__." id=".$localobject->id." result=".$result." ref=".$localobject->ref."\n";
$this->assertEquals(Mo::STATUS_VALIDATED, $localobject->status);
$this->assertNotEquals($oldref, $localobject->ref, 'validate() must replace the provisional ref with a definitive one');
$this->assertNotRegExp('/^\(?PROV/i', $localobject->ref);
return $localobject;
}
/**
* testMoCancel
*
* @param Mo $localobject Mo
* @return Mo
*
* @depends testMoValidate
* The depends says test is run only if previous is ok
*/
public function testMoCancel($localobject)
{
global $conf,$user,$langs,$db;
$conf = $this->savconf;
$user = $this->savuser;
$langs = $this->savlangs;
$db = $this->savdb;
$result = $localobject->cancel($user);
$this->assertGreaterThan(0, $result, $localobject->errorsToString());
print __METHOD__." id=".$localobject->id." result=".$result."\n";
$localobject->fetch($localobject->id);
$this->assertEquals(Mo::STATUS_CANCELED, $localobject->status);
return $localobject;
}
/**
* testMoReopen
*
* @param Mo $localobject Mo
* @return int
*
* @depends testMoCancel
* The depends says test is run only if previous is ok
*/
public function testMoReopen($localobject)
{
global $conf,$user,$langs,$db;
$conf = $this->savconf;
$user = $this->savuser;
$langs = $this->savlangs;
$db = $this->savdb;
$result = $localobject->reopen($user);
$this->assertGreaterThan(0, $result, $localobject->errorsToString());
print __METHOD__." id=".$localobject->id." result=".$result."\n";
$localobject->fetch($localobject->id);
$this->assertEquals(Mo::STATUS_VALIDATED, $localobject->status);
return $localobject->id;
}
/**
* testMoDelete
*
* @param int $id Id of object
* @return int
*
* @depends testMoReopen
* The depends says test is run only if previous is ok
*/
public function testMoDelete($id)
{
global $conf,$user,$langs,$db;
$conf = $this->savconf;
$user = $this->savuser;
$langs = $this->savlangs;
$db = $this->savdb;
$localobject = new Mo($db);
$result = $localobject->fetch($id);
$result = $localobject->delete($user);
$this->assertGreaterThan(0, $result, $localobject->errorsToString());
print __METHOD__." id=".$id." result=".$result."\n";
return $result;
}
}