From c49ffbb5ffae8053d20f60511706ac93b1a2f5fb Mon Sep 17 00:00:00 2001 From: Dave MacFarlane Date: Mon, 14 Dec 2020 14:09:19 -0500 Subject: [PATCH 01/23] [NDB_Factory] Remove testing mode The "Testing" mode in NDB_Factory complicates our code and frequently confuses phan. It appears to not be used by our testing suite, and the test suite often explicitly sets it to not-test mode in order to get real connections, and uses setDatabase/setConfig/etc to inject mocks rather than using the factory's internal test mode. This removes the mode and simplifies our code. --- php/libraries/NDB_Factory.class.inc | 150 ++++++---------------------- 1 file changed, 32 insertions(+), 118 deletions(-) diff --git a/php/libraries/NDB_Factory.class.inc b/php/libraries/NDB_Factory.class.inc index f408a9a90d6..09bc9ab9816 100644 --- a/php/libraries/NDB_Factory.class.inc +++ b/php/libraries/NDB_Factory.class.inc @@ -4,8 +4,7 @@ * are usually singletons. Instead of directly calling class::singleton staticly, * this factory should be used so that a mock class can be subbed in for testing. * - * If the Factory is in testing mode (setTesting(true) was called), a mock will - * be returned. Otherwise, the normal NDB_ prefixed object will be returned. + * Mocks are injected using the setDatabase/setConfig/etc methods. * * PHP Version 7 * @@ -43,9 +42,6 @@ class NDB_Factory private static $_timepoints = []; private $_baseURL = ""; - var $Testing; // Whether or not Mock objects should be returned instead of - // real ones - // /** * Settings object @@ -55,18 +51,6 @@ class NDB_Factory */ private static $_settings = null; - /** - * Sets whether the factory should return real objects or testing objects - * - * @param boolean $val Whether testing should be enabled - * - * @return void - */ - function setTesting(bool $val): void - { - $this->Testing = $val; - } - /** * Returns a single factory object. This must be used instead of being * constructed directly so that the testing suite and Loris code are @@ -117,34 +101,14 @@ class NDB_Factory */ function config(?string $configfile = '../project/config.xml'): \NDB_Config { - if (self::$config !== null) { - // The below suppression is necessary to satisfy phan. It flags an - // error here because the function is declared to return - // NDB_Config and phan doesn't understand MockNDB_Config (as it is - // generated dynamically by PHPUnit). - // We know this will always return \NDB_Config since this is guaranteed - // by the return type of the function. - // - // @phan-suppress-next-line PhanTypeMismatchReturn - return self::$config; - } - if ($this->Testing) { - $config = new MockNDB_Config(); - $configfile = '../test/config.xml'; - } else { - $config = NDB_Config::singleton($configfile); + $config = self::$config; + if ($config !== null) { + return $config; } - self::$config = $config; + $config = NDB_Config::singleton($configfile); - // The below suppression is necessary to satisfy phan. It flags an - // error here because the function is declared to return - // NDB_Config and phan doesn't understand MockNDB_Config (as it is - // generated dynamically by PHPUnit). - // We know this will always return \NDB_Config since this is guaranteed - // by the return type of the function. - // - // @phan-suppress-next-line PhanTypeMismatchReturn + self::$config = $config; return $config; } @@ -169,22 +133,11 @@ class NDB_Factory */ function user(): \User { - if (self::$_user !== null) { - // The below suppression is necessary to satisfy phan. It flags an - // error here because the function is declared to return - // User and phan doesn't understand MockUser (as it is - // generated dynamically by PHPUnit). - // We know this will always return \User since this is guaranteed - // by the return type of the function. - // @phan-suppress-next-line PhanTypeMismatchReturn - return self::$_user; - } - if ($this->Testing) { - Mock::generate("User"); - $user = new MockUser(); - } else { - $user = \User::singleton(); + $user = self::$_user; + if ($user !== null) { + return $user; } + $user = \User::singleton(); self::$_user = $user; return $user; } @@ -210,21 +163,11 @@ class NDB_Factory */ function database(): \Database { - $db_ref = null; - if ($this->Testing) { - $db_ref = &self::$testdb; - if ($db_ref !== null) { - return $db_ref; - } - //self::$testdb = Mock::generate("Database"); - self::$testdb = new MockDatabase(); - } else { - $db_ref = &self::$db; - if ($db_ref !== null) { - return $db_ref; - } - self::$db = Database::singleton(); + $db_ref = &self::$db; + if ($db_ref !== null) { + return $db_ref; } + $config = $this->config(); $dbc = $config->getSetting('database'); // This check was added to satisfy phan, our static analysis tool. @@ -235,13 +178,15 @@ class NDB_Factory 'Could not configure database for LORIS' ); } - $db_ref->connect( + + $db_ref = \Database::singleton( $dbc['database'], $dbc['username'], $dbc['password'], $dbc['host'], true ); + self::$db = $db_ref; return $db_ref; } @@ -339,29 +284,16 @@ class NDB_Factory if (!empty(self::$_couchdb[$database])) { return self::$_couchdb[$database]; } - if ($this->Testing) { - Mock::generatePartial( - 'CouchDB', - 'MockCouchDBWrap', - /* mock out the functions that make HTTP requests */ - [ - '_getRelativeURL', - '_postRelativeURL', - '_postURL', - '_getURL', - ] - ); - self::$_couchdb[$database] = new MockCouchDBWrap(); - } else { - self::$_couchdb[$database] = CouchDB::getInstance( - $database, - $host, - $port, - $user, - $password - ); - } - return self::$_couchdb[$database]; + + $couch = CouchDB::getInstance( + $database, + $host, + $port, + $user, + $password + ); + self::$_couchdb[$database] = $couch; + return $couch; } /** @@ -419,15 +351,7 @@ class NDB_Factory */ public function project(string $projectName): \Project { - $project = null; - - if ($this->Testing) { - $project = new MockProject($projectName); - } else { - $project = \Project::singleton($projectName); - } - - return $project; + return \Project::singleton($projectName); } /** @@ -443,11 +367,7 @@ class NDB_Factory if (isset(self::$_candidates[$key])) { return self::$_candidates[$key]; } - if ($this->Testing) { - self::$_candidates[$key] = new MockCandidate($CandID); - } else { - self::$_candidates[$key] = Candidate::singleton($CandID); - } + self::$_candidates[$key] = Candidate::singleton($CandID); return self::$_candidates[$key]; } @@ -465,15 +385,9 @@ class NDB_Factory if (isset(self::$_timepoints[(string) $sessionID])) { return self::$_timepoints[(string) $sessionID]; } - if ($this->Testing) { - self::$_timepoints[(string) $sessionID] = new MockTimepoint( - $sessionID - ); - } else { - self::$_timepoints[(string) $sessionID] = \TimePoint::singleton( - $sessionID - ); - } + self::$_timepoints[(string) $sessionID] = \TimePoint::singleton( + $sessionID + ); return self::$_timepoints[(string) $sessionID]; } } From 4e36246e50731721d9cd4e85a87782730b508ad3 Mon Sep 17 00:00:00 2001 From: Dave MacFarlane Date: Mon, 14 Dec 2020 14:15:27 -0500 Subject: [PATCH 02/23] PHPCS --- php/libraries/NDB_Factory.class.inc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/php/libraries/NDB_Factory.class.inc b/php/libraries/NDB_Factory.class.inc index 09bc9ab9816..08eda9a4830 100644 --- a/php/libraries/NDB_Factory.class.inc +++ b/php/libraries/NDB_Factory.class.inc @@ -137,7 +137,7 @@ class NDB_Factory if ($user !== null) { return $user; } - $user = \User::singleton(); + $user = \User::singleton(); self::$_user = $user; return $user; } @@ -186,6 +186,7 @@ class NDB_Factory $dbc['host'], true ); + self::$db = $db_ref; return $db_ref; } From 59e196fe731077578c44ba8fe0c5c9d08eaa0c38 Mon Sep 17 00:00:00 2001 From: Dave MacFarlane Date: Mon, 14 Dec 2020 14:25:44 -0500 Subject: [PATCH 03/23] Remove calls to setTesting --- test/integrationtests/LorisIntegrationTest.class.inc | 1 - test/unittests/CandidateTest.php | 1 - test/unittests/Database_Test.php | 1 - test/unittests/NDB_BVL_Instrument_LINST_ToJSON_Test.php | 1 - test/unittests/NDB_BVL_Instrument_Test.php | 8 +++----- test/unittests/NDB_Factory_Test.php | 9 +-------- 6 files changed, 4 insertions(+), 17 deletions(-) diff --git a/test/integrationtests/LorisIntegrationTest.class.inc b/test/integrationtests/LorisIntegrationTest.class.inc index e03eb7ec481..a90e7d52859 100755 --- a/test/integrationtests/LorisIntegrationTest.class.inc +++ b/test/integrationtests/LorisIntegrationTest.class.inc @@ -64,7 +64,6 @@ abstract class LorisIntegrationTest extends TestCase // Set up database wrapper and config $this->factory = NDB_Factory::singleton(); $this->factory->reset(); - $this->factory->setTesting(false); $this->config = $this->factory->Config(CONFIG_XML); diff --git a/test/unittests/CandidateTest.php b/test/unittests/CandidateTest.php index 1a3d9b86c2f..abf104f08e7 100644 --- a/test/unittests/CandidateTest.php +++ b/test/unittests/CandidateTest.php @@ -1311,7 +1311,6 @@ private function _setUpMockDB() { $this->_factoryForDB = NDB_Factory::singleton(); $this->_factoryForDB->reset(); - $this->_factoryForDB->setTesting(false); $this->_config = $this->_factoryForDB->Config(CONFIG_XML); $database = $this->_config->getSetting('database'); $this->_DB = Database::singleton( diff --git a/test/unittests/Database_Test.php b/test/unittests/Database_Test.php index e8869c33fb2..dd70662be35 100644 --- a/test/unittests/Database_Test.php +++ b/test/unittests/Database_Test.php @@ -86,7 +86,6 @@ protected function setUp(): void { $this->factory = NDB_Factory::singleton(); $this->factory->reset(); - $this->factory->setTesting(false); $this->config = $this->factory->Config(CONFIG_XML); $database = $this->config->getSetting('database'); $this->DB = Database::singleton( diff --git a/test/unittests/NDB_BVL_Instrument_LINST_ToJSON_Test.php b/test/unittests/NDB_BVL_Instrument_LINST_ToJSON_Test.php index 04c80e9d87e..afabb9faccb 100644 --- a/test/unittests/NDB_BVL_Instrument_LINST_ToJSON_Test.php +++ b/test/unittests/NDB_BVL_Instrument_LINST_ToJSON_Test.php @@ -50,7 +50,6 @@ function setUp(): void ]; $factory = \NDB_Factory::singleton(); - $factory->setTesting(true); $mockdb = $this->getMockBuilder("\Database")->getMock(); $mockconfig = $this->getMockBuilder("\NDB_Config")->getMock(); diff --git a/test/unittests/NDB_BVL_Instrument_Test.php b/test/unittests/NDB_BVL_Instrument_Test.php index a3147891dcb..5d439fb1ac8 100644 --- a/test/unittests/NDB_BVL_Instrument_Test.php +++ b/test/unittests/NDB_BVL_Instrument_Test.php @@ -65,14 +65,12 @@ function setUp(): void ]; $this->_factory = \NDB_Factory::singleton(); - $this->_factory->setTesting(true); $this->_mockDB = $this->getMockBuilder("\Database")->getMock(); $this->_mockConfig = $this->getMockBuilder("\NDB_Config")->getMock(); - \NDB_Factory::$db = $this->_mockDB; - \NDB_Factory::$testdb = $this->_mockDB; - \NDB_Factory::$config = $this->_mockConfig; + $this->_factory->setDatabase($this->_mockDB); + $this->_factory->setConfig($this->_mockConfig); $this->quickForm = new \LorisForm(); @@ -1986,7 +1984,7 @@ private function _setUpMockDB() { $this->_factoryForDB = \NDB_Factory::singleton(); $this->_factoryForDB->reset(); - $this->_factoryForDB->setTesting(false); + $this->_config = $this->_factoryForDB->Config(CONFIG_XML); $database = $this->_config->getSetting('database'); $this->_DB = \Database::singleton( diff --git a/test/unittests/NDB_Factory_Test.php b/test/unittests/NDB_Factory_Test.php index f16c94fc4ee..acee55624d9 100644 --- a/test/unittests/NDB_Factory_Test.php +++ b/test/unittests/NDB_Factory_Test.php @@ -49,12 +49,11 @@ protected function setUp(): void /** * Test that the singleton function returns a new NDB_Factory object and - * that the reset and setTesting functions properly set the properties of + * that the reset function properly set the properties of * the object * * @covers NDB_Factory::singleton * @covers NDB_Factory::reset - * @covers NDB_Factory::setTesting * @return void */ function testSetUp() @@ -64,8 +63,6 @@ function testSetUp() $this->assertNull(\NDB_Factory::$testdb); $this->assertNull(\NDB_Factory::$db); $this->assertNull(\NDB_Factory::$config); - $this->_factory->setTesting(true); - $this->assertTrue($this->_factory->Testing); } /** @@ -76,7 +73,6 @@ function testSetUp() */ function testConfig() { - $this->_factory->setTesting(false); $this->_factory->config(); $this->assertEquals( NDB_Config::singleton('../project/config.xml'), @@ -106,7 +102,6 @@ function testSetConfig() */ function testUser() { - $this->_factory->setTesting(false); $this->assertEquals( \User::singleton(), $this->_factory->user() @@ -136,7 +131,6 @@ function testSetUser() */ function testDatabase() { - $this->_factory->setTesting(false); $this->assertEquals($this->_DB, $this->_factory->database()); } @@ -176,7 +170,6 @@ function testCouchDBThrowsException() */ function testCouchDB() { - $this->_factory->setTesting(false); $this->assertEquals( CouchDB::getInstance("db", "host", 1, "user", "pass"), $this->_factory->couchDB("db", "host", 1, "user", "pass") From e788ad4913c874bb738967e1f67c0c7068b61e1b Mon Sep 17 00:00:00 2001 From: Dave MacFarlane Date: Mon, 14 Dec 2020 14:30:23 -0500 Subject: [PATCH 04/23] Do not suppress phan --- php/libraries/NDB_Factory.class.inc | 5 ----- test/unittests/NDB_Factory_Test.php | 2 +- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/php/libraries/NDB_Factory.class.inc b/php/libraries/NDB_Factory.class.inc index 08eda9a4830..c65759e4980 100644 --- a/php/libraries/NDB_Factory.class.inc +++ b/php/libraries/NDB_Factory.class.inc @@ -24,11 +24,6 @@ use \LORIS\StudyEntities\Candidate\CandID; * @author Dave MacFarlane * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 * @link https://www.github.com/aces/Loris/ - * - * Phan currently has bad support for PHPUnit's Mock objects which is - * creating false positive with the below rule. - * - * @phan-file-suppress PhanUndeclaredClassMethod */ class NDB_Factory { diff --git a/test/unittests/NDB_Factory_Test.php b/test/unittests/NDB_Factory_Test.php index acee55624d9..2df747d02db 100644 --- a/test/unittests/NDB_Factory_Test.php +++ b/test/unittests/NDB_Factory_Test.php @@ -251,4 +251,4 @@ function testTimepoint() $this->_factory->timepoint($sessionID) ); } -} \ No newline at end of file +} From ea5c90e93d183132e7e505b88a812d9bdfcb021b Mon Sep 17 00:00:00 2001 From: Dave MacFarlane Date: Mon, 14 Dec 2020 14:34:44 -0500 Subject: [PATCH 05/23] More setTesting calls --- test/unittests/CandidateTest.php | 1 + test/unittests/SinglePointLoginTest.php | 1 - test/unittests/UserTest.php | 1 - test/unittests/VisitTest.php | 1 - 4 files changed, 1 insertion(+), 3 deletions(-) diff --git a/test/unittests/CandidateTest.php b/test/unittests/CandidateTest.php index abf104f08e7..3cea2c92f89 100644 --- a/test/unittests/CandidateTest.php +++ b/test/unittests/CandidateTest.php @@ -148,6 +148,7 @@ protected function setUp(): void $this->_configMock = $this->getMockBuilder('NDB_Config')->getMock(); $this->_dbMock = $this->getMockBuilder('Database')->getMock(); $this->_factory = NDB_Factory::singleton(); + $this->_factory->setConfig($this->_configMock); $this->_factory->setDatabase($this->_dbMock); diff --git a/test/unittests/SinglePointLoginTest.php b/test/unittests/SinglePointLoginTest.php index b7ca86d05d0..25e5006cac3 100644 --- a/test/unittests/SinglePointLoginTest.php +++ b/test/unittests/SinglePointLoginTest.php @@ -35,7 +35,6 @@ class SinglePointLoginTest extends TestCase protected function setUp(): void { $Factory = NDB_Factory::singleton(); - $Factory->setTesting(true); $mockdb = $this->getMockBuilder("\Database")->getMock(); $mockconfig = $this->getMockBuilder("\NDB_Config")->getMock(); diff --git a/test/unittests/UserTest.php b/test/unittests/UserTest.php index 61744282156..5091714ad6c 100644 --- a/test/unittests/UserTest.php +++ b/test/unittests/UserTest.php @@ -262,7 +262,6 @@ protected function setUp(): void parent::setUp(); $this->_factory = \NDB_Factory::singleton(); $this->_factory->reset(); - $this->_factory->setTesting(false); $this->_configMock = $this->_factory->Config(CONFIG_XML); $database = $this->_configMock->getSetting('database'); $this->_dbMock = \Database::singleton( diff --git a/test/unittests/VisitTest.php b/test/unittests/VisitTest.php index 442b491eed5..bea9b852a8e 100644 --- a/test/unittests/VisitTest.php +++ b/test/unittests/VisitTest.php @@ -50,7 +50,6 @@ protected function setUp(): void { $this->factory = NDB_Factory::singleton(); $this->factory->reset(); - $this->factory->setTesting(false); $this->config = $this->factory->Config(CONFIG_XML); $database = $this->config->getSetting('database'); $this->DB = Database::singleton( From 596c6b7d8a58ef1451fa59a1e475814d5b1c3687 Mon Sep 17 00:00:00 2001 From: Dave MacFarlane Date: Mon, 14 Dec 2020 14:40:14 -0500 Subject: [PATCH 06/23] phpcs --- test/unittests/SinglePointLoginTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unittests/SinglePointLoginTest.php b/test/unittests/SinglePointLoginTest.php index 25e5006cac3..04d6df033b1 100644 --- a/test/unittests/SinglePointLoginTest.php +++ b/test/unittests/SinglePointLoginTest.php @@ -34,7 +34,7 @@ class SinglePointLoginTest extends TestCase */ protected function setUp(): void { - $Factory = NDB_Factory::singleton(); + $Factory = NDB_Factory::singleton(); $mockdb = $this->getMockBuilder("\Database")->getMock(); $mockconfig = $this->getMockBuilder("\NDB_Config")->getMock(); From 872ae41272994bba6bb95bd72c9a1079ae01fc11 Mon Sep 17 00:00:00 2001 From: Dave MacFarlane Date: Mon, 14 Dec 2020 14:45:16 -0500 Subject: [PATCH 07/23] one more settesting --- test/unittests/UtilityTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/test/unittests/UtilityTest.php b/test/unittests/UtilityTest.php index 9d91ba89fb4..235a89c11a7 100644 --- a/test/unittests/UtilityTest.php +++ b/test/unittests/UtilityTest.php @@ -1216,7 +1216,6 @@ private function _setMockDB() { $this->_mockFactory = \NDB_Factory::singleton(); $this->_mockFactory->reset(); - $this->_mockFactory->setTesting(false); $this->_mockConfig = $this->_mockFactory->Config(CONFIG_XML); $database = $this->_mockConfig->getSetting('database'); $this->_mockDB = \Database::singleton( From ab9b3013fd56e3a60b95f820acd908cc4cdddf1b Mon Sep 17 00:00:00 2001 From: Dave MacFarlane Date: Mon, 14 Dec 2020 14:54:34 -0500 Subject: [PATCH 08/23] Add some missing sets --- test/unittests/CandidateTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/unittests/CandidateTest.php b/test/unittests/CandidateTest.php index 3cea2c92f89..1b93d6c35fb 100644 --- a/test/unittests/CandidateTest.php +++ b/test/unittests/CandidateTest.php @@ -1321,5 +1321,8 @@ private function _setUpMockDB() $database['host'], 1 ); + + $this->_factoryForDB->setDatabase($this->_DB); + $this->_factoryForDB->setConfig($this->_config); } } From 6d8086a80f19394e57697481dee841e5ba6178fc Mon Sep 17 00:00:00 2001 From: Dave MacFarlane Date: Mon, 14 Dec 2020 15:03:12 -0500 Subject: [PATCH 09/23] Missing config in database test --- test/unittests/Database_Test.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/unittests/Database_Test.php b/test/unittests/Database_Test.php index dd70662be35..edef89e0bf5 100644 --- a/test/unittests/Database_Test.php +++ b/test/unittests/Database_Test.php @@ -95,6 +95,9 @@ protected function setUp(): void $database['host'], 1 ); + + $this->factory->setDatabase($this->DB); + $this->factory->setConfig($this->config); } /** From 141628b154c45dd159dcef3dbdad66ec296d6466 Mon Sep 17 00:00:00 2001 From: Dave MacFarlane Date: Mon, 14 Dec 2020 15:04:55 -0500 Subject: [PATCH 10/23] missing configs --- test/unittests/NDB_BVL_Instrument_Test.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/unittests/NDB_BVL_Instrument_Test.php b/test/unittests/NDB_BVL_Instrument_Test.php index 5d439fb1ac8..1f0c5eeadec 100644 --- a/test/unittests/NDB_BVL_Instrument_Test.php +++ b/test/unittests/NDB_BVL_Instrument_Test.php @@ -1994,6 +1994,9 @@ private function _setUpMockDB() $database['host'], 1 ); + + $this->_factoryForDB->setDatabase($this->_DB); + $this->_factoryForDB->setConfig($this->_config); } } From 0170f11a813ea619dfbdac12c40e7e247743c936 Mon Sep 17 00:00:00 2001 From: Dave MacFarlane Date: Mon, 14 Dec 2020 15:22:11 -0500 Subject: [PATCH 11/23] More setters --- test/unittests/NDB_Factory_Test.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/unittests/NDB_Factory_Test.php b/test/unittests/NDB_Factory_Test.php index 2df747d02db..867fa155361 100644 --- a/test/unittests/NDB_Factory_Test.php +++ b/test/unittests/NDB_Factory_Test.php @@ -45,6 +45,9 @@ protected function setUp(): void $database['host'], true ); + + $this->factory->setDatabase($this->_DB); + $this->factory->setConfig($this->_config); } /** From a0ce835da1bb55fc346f753e148a72181fdcf7a7 Mon Sep 17 00:00:00 2001 From: Dave MacFarlane Date: Mon, 14 Dec 2020 15:23:18 -0500 Subject: [PATCH 12/23] And another --- test/unittests/UtilityTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/unittests/UtilityTest.php b/test/unittests/UtilityTest.php index 235a89c11a7..d3f39f19ce6 100644 --- a/test/unittests/UtilityTest.php +++ b/test/unittests/UtilityTest.php @@ -1225,5 +1225,8 @@ private function _setMockDB() $database['host'], true ); + + $this->_mockFactory->setDatabase($this->_mockDB); + $this->_mockFactory->setConfig($this->_mockConfig); } } From 86e02aa8c82aca484ab2f97140b0bbd7623de7c6 Mon Sep 17 00:00:00 2001 From: Dave MacFarlane Date: Mon, 14 Dec 2020 15:33:28 -0500 Subject: [PATCH 13/23] Set factory in integration tests --- test/integrationtests/LorisIntegrationTest.class.inc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/integrationtests/LorisIntegrationTest.class.inc b/test/integrationtests/LorisIntegrationTest.class.inc index a90e7d52859..b9733463757 100755 --- a/test/integrationtests/LorisIntegrationTest.class.inc +++ b/test/integrationtests/LorisIntegrationTest.class.inc @@ -76,6 +76,10 @@ abstract class LorisIntegrationTest extends TestCase $database['host'], 1 ); + + $this->factory->setDatabase($this->DB); + $this->factory->setConfig($this->config); + $this->url = getenv('DOCKER_WEB_SERVER'); $password = new \Password($this->validPassword); From e6607312cbac4c66a27b7f55e6a458290e53633e Mon Sep 17 00:00:00 2001 From: Dave MacFarlane Date: Mon, 14 Dec 2020 15:35:59 -0500 Subject: [PATCH 14/23] Set the factory in NDB_Client --- php/libraries/NDB_Client.class.inc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/php/libraries/NDB_Client.class.inc b/php/libraries/NDB_Client.class.inc index ccc9a757ec6..ec5bac5099d 100644 --- a/php/libraries/NDB_Client.class.inc +++ b/php/libraries/NDB_Client.class.inc @@ -71,6 +71,9 @@ class NDB_Client // singleton reference $DB = $factory->database(); + $factory->setConfig($config); + $factory->setDatabase($DB); + // add extra include paths. This must be done // after the database is connected, since the setting // can come from the database. From 0a124cd787e1fca79c73a4e50aaa3e306e5c8226 Mon Sep 17 00:00:00 2001 From: Dave MacFarlane Date: Mon, 14 Dec 2020 15:37:04 -0500 Subject: [PATCH 15/23] Explicitly set database in NDB_Client --- php/libraries/NDB_Client.class.inc | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/php/libraries/NDB_Client.class.inc b/php/libraries/NDB_Client.class.inc index ec5bac5099d..bf8636c234e 100644 --- a/php/libraries/NDB_Client.class.inc +++ b/php/libraries/NDB_Client.class.inc @@ -65,14 +65,13 @@ class NDB_Client $DBSettings['host'], true ); + // Now make sure the factory reference is the same as the + // singleton reference + $factory->setDatabase($DB); } - // Now make sure the factory reference is the same as the - // singleton reference $DB = $factory->database(); - $factory->setConfig($config); - $factory->setDatabase($DB); // add extra include paths. This must be done // after the database is connected, since the setting From 6959d7b9e943c9263be1c24f27c037bdde7b71a2 Mon Sep 17 00:00:00 2001 From: Dave MacFarlane Date: Mon, 14 Dec 2020 15:41:46 -0500 Subject: [PATCH 16/23] phpcs --- php/libraries/NDB_Client.class.inc | 1 - test/integrationtests/LorisIntegrationTest.class.inc | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/php/libraries/NDB_Client.class.inc b/php/libraries/NDB_Client.class.inc index bf8636c234e..23497978126 100644 --- a/php/libraries/NDB_Client.class.inc +++ b/php/libraries/NDB_Client.class.inc @@ -72,7 +72,6 @@ class NDB_Client $DB = $factory->database(); - // add extra include paths. This must be done // after the database is connected, since the setting // can come from the database. diff --git a/test/integrationtests/LorisIntegrationTest.class.inc b/test/integrationtests/LorisIntegrationTest.class.inc index b9733463757..401b8abb70e 100755 --- a/test/integrationtests/LorisIntegrationTest.class.inc +++ b/test/integrationtests/LorisIntegrationTest.class.inc @@ -69,7 +69,7 @@ abstract class LorisIntegrationTest extends TestCase $database = $this->config->getSetting('database'); - $this->DB = Database::singleton( + $this->DB = Database::singleton( $database['database'], $database['username'], $database['password'], From d8117df63df0e318cb20cb13bbf0a1a5548ed59d Mon Sep 17 00:00:00 2001 From: Dave MacFarlane Date: Mon, 14 Dec 2020 15:48:12 -0500 Subject: [PATCH 17/23] Typo --- test/unittests/NDB_Factory_Test.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unittests/NDB_Factory_Test.php b/test/unittests/NDB_Factory_Test.php index 867fa155361..4bcc75d7f56 100644 --- a/test/unittests/NDB_Factory_Test.php +++ b/test/unittests/NDB_Factory_Test.php @@ -46,8 +46,8 @@ protected function setUp(): void true ); - $this->factory->setDatabase($this->_DB); - $this->factory->setConfig($this->_config); + $this->_factory->setDatabase($this->_DB); + $this->_factory->setConfig($this->_config); } /** From 9fd7b0dd2419ac8de0cb32100f9b03f244b8c2c4 Mon Sep 17 00:00:00 2001 From: Dave MacFarlane Date: Mon, 14 Dec 2020 15:56:01 -0500 Subject: [PATCH 18/23] More sets --- test/unittests/NDB_Factory_Test.php | 1 + 1 file changed, 1 insertion(+) diff --git a/test/unittests/NDB_Factory_Test.php b/test/unittests/NDB_Factory_Test.php index 4bcc75d7f56..9405afe8804 100644 --- a/test/unittests/NDB_Factory_Test.php +++ b/test/unittests/NDB_Factory_Test.php @@ -44,6 +44,7 @@ protected function setUp(): void $database['password'], $database['host'], true + ); $this->_factory->setDatabase($this->_DB); From aae2395743640392c2201790a8f7f64488ed576e Mon Sep 17 00:00:00 2001 From: Dave MacFarlane Date: Mon, 14 Dec 2020 16:03:31 -0500 Subject: [PATCH 19/23] Test --- test/unittests/NDB_Factory_Test.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unittests/NDB_Factory_Test.php b/test/unittests/NDB_Factory_Test.php index 9405afe8804..139138fce69 100644 --- a/test/unittests/NDB_Factory_Test.php +++ b/test/unittests/NDB_Factory_Test.php @@ -36,7 +36,8 @@ protected function setUp(): void { parent::setUp(); $this->_factory = \NDB_Factory::singleton(); - $this->_config = \NDB_Config::singleton(); + $this->_config = \NDB_Config::singleton('../project/config.xml'); + $database = $this->_config->getSetting('database'); $this->_DB = Database::singleton( $database['database'], @@ -44,7 +45,6 @@ protected function setUp(): void $database['password'], $database['host'], true - ); $this->_factory->setDatabase($this->_DB); From 3fda1ced5700d9bce271ecc2fd6c53837d1c3abc Mon Sep 17 00:00:00 2001 From: Dave MacFarlane Date: Mon, 14 Dec 2020 16:06:01 -0500 Subject: [PATCH 20/23] Use same structure as Database tests --- test/unittests/NDB_Factory_Test.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/unittests/NDB_Factory_Test.php b/test/unittests/NDB_Factory_Test.php index 139138fce69..cb343bfcac8 100644 --- a/test/unittests/NDB_Factory_Test.php +++ b/test/unittests/NDB_Factory_Test.php @@ -35,11 +35,12 @@ class NDB_Factory_Test extends TestCase protected function setUp(): void { parent::setUp(); - $this->_factory = \NDB_Factory::singleton(); - $this->_config = \NDB_Config::singleton('../project/config.xml'); + $this->_factory = NDB_Factory::singleton(); + $this->_factory->reset(); - $database = $this->_config->getSetting('database'); - $this->_DB = Database::singleton( + $this->_config = $this->factory->Config(CONFIG_XML); + $database = $this->config->getSetting('database'); + $this->_DB = Database::singleton( $database['database'], $database['username'], $database['password'], From 265ed8c23adfa824066e9a7e22d5d5d7e84ac7fe Mon Sep 17 00:00:00 2001 From: Dave MacFarlane Date: Mon, 14 Dec 2020 16:15:20 -0500 Subject: [PATCH 21/23] typo --- test/unittests/NDB_Factory_Test.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unittests/NDB_Factory_Test.php b/test/unittests/NDB_Factory_Test.php index cb343bfcac8..2ef5b205dd3 100644 --- a/test/unittests/NDB_Factory_Test.php +++ b/test/unittests/NDB_Factory_Test.php @@ -38,8 +38,8 @@ protected function setUp(): void $this->_factory = NDB_Factory::singleton(); $this->_factory->reset(); - $this->_config = $this->factory->Config(CONFIG_XML); - $database = $this->config->getSetting('database'); + $this->_config = $this->_factory->Config(CONFIG_XML); + $database = $this->_config->getSetting('database'); $this->_DB = Database::singleton( $database['database'], $database['username'], From 3bf30579446d70237f1ca339435548953ad31cb3 Mon Sep 17 00:00:00 2001 From: Dave MacFarlane Date: Wed, 16 Dec 2020 09:46:03 -0500 Subject: [PATCH 22/23] Use mocks for failing tests --- test/unittests/NDB_Factory_Test.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/unittests/NDB_Factory_Test.php b/test/unittests/NDB_Factory_Test.php index 2ef5b205dd3..0596eb2e412 100644 --- a/test/unittests/NDB_Factory_Test.php +++ b/test/unittests/NDB_Factory_Test.php @@ -234,6 +234,12 @@ function testProject() */ function testCandidate() { + $mockdb = $this->getMockBuilder("\Database")->getMock(); + $this->_factory->setDatabase($mockdb); + $mockdb->expects($this->any()) + ->method('pselectRow') + ->willReturn(['DCCID'=>'300001']); + $candID = new CandID("300001"); $this->assertEquals( Candidate::singleton($candID), @@ -250,6 +256,14 @@ function testCandidate() */ function testTimepoint() { + $mockdb = $this->getMockBuilder("\Database")->getMock(); + $mockconfig = $this->getMockBuilder("\NDB_Config")->getMock(); + $this->_factory->setConfig($mockconfig); + $this->_factory->setDatabase($mockdb); + $mockdb->expects($this->any()) + ->method('pselectRow') + ->willReturn(['1']); + $sessionID = new \SessionID("1"); $this->assertEquals( \TimePoint::singleton($sessionID), From 81deef5b047e182982b4dc97185f73865ab02365 Mon Sep 17 00:00:00 2001 From: Dave MacFarlane Date: Wed, 16 Dec 2020 10:19:33 -0500 Subject: [PATCH 23/23] Do not depend on RB for utility test --- php/libraries/Utility.class.inc | 4 +++- test/unittests/UtilityTest.php | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/php/libraries/Utility.class.inc b/php/libraries/Utility.class.inc index 11b51d46d4c..29761309e2b 100644 --- a/php/libraries/Utility.class.inc +++ b/php/libraries/Utility.class.inc @@ -851,8 +851,10 @@ class Utility "Input must be a valid date/time string: $e" ); } + + $factory = \NDB_Factory::singleton(); return $dt->format( - \NDB_Config::singleton()->getSetting('dateDisplayFormat') + $factory->config()->getSetting('dateDisplayFormat') ?? DateTime::ATOM ); } diff --git a/test/unittests/UtilityTest.php b/test/unittests/UtilityTest.php index d3f39f19ce6..56cfafc0962 100644 --- a/test/unittests/UtilityTest.php +++ b/test/unittests/UtilityTest.php @@ -1136,6 +1136,13 @@ public function testRandomString() public function testToDateDisplayFormat() { $this->_setMockDB(); + + $config = $this->getMockBuilder("\NDB_Config")->getMock(); + $config->expects($this->any()) + ->method('getSetting') + ->willReturn('Y-m-d H:i:s'); + $this->_mockFactory->setConfig($config); + $date = "2000-01-01"; $this->assertEquals( "2000-01-01 00:00:00",