From 42e0d3424a8ee561075e4c6ae1c3257d60654d7c Mon Sep 17 00:00:00 2001 From: Dillon-Brown Date: Wed, 4 Sep 2019 02:48:02 +0000 Subject: [PATCH] Fixed #7740 - Discussion on replacing the StateChecker with database truncation in tests --- include/DatabaseTransactions.php | 3 +- include/RefreshDatabase.php | 16 ++++-- include/TestCaseAbstract.php | 21 ++++--- ....php => SuitePHPUnitFrameworkTestCase.php} | 30 ++++++---- .../Api/Core/Loader/CustomLoaderTest.php | 4 +- tests/unit/phpunit/configTest.php | 4 +- tests/unit/phpunit/data/SugarBeanTest.php | 4 +- .../phpunit/include/APIErrorObjectTest.php | 4 +- .../unit/phpunit/include/ErrorMessageTest.php | 4 +- .../include/Imap/ImapHandlerFactoryTest.php | 4 +- .../Imap/ImapTestSettingsEntryHandlerTest.php | 4 +- .../phpunit/include/LangExceptionTest.php | 4 +- tests/unit/phpunit/include/LangTextTest.php | 4 +- .../MVC/Controller/ControllerFactoryTest.php | 4 +- .../MVC/Controller/SugarControllerTest.php | 4 +- .../include/MVC/SugarApplicationTest.php | 4 +- .../phpunit/include/MVC/SugarModuleTest.php | 4 +- .../include/MVC/View/SugarViewTest.php | 4 +- .../include/MVC/View/ViewFactoryTest.php | 4 +- .../include/MVC/View/views/view.ajaxTest.php | 4 +- .../MVC/View/views/view.ajaxuiTest.php | 4 +- .../MVC/View/views/view.classicTest.php | 4 +- .../MVC/View/views/view.detailTest.php | 4 +- .../include/MVC/View/views/view.editTest.php | 4 +- .../MVC/View/views/view.favoritesTest.php | 4 +- .../include/MVC/View/views/view.htmlTest.php | 4 +- .../MVC/View/views/view.importvcardTest.php | 4 +- .../View/views/view.importvcardsaveTest.php | 4 +- .../include/MVC/View/views/view.jsonTest.php | 4 +- .../include/MVC/View/views/view.listTest.php | 4 +- .../MVC/View/views/view.metadataTest.php | 4 +- .../View/views/view.modulelistmenuTest.php | 4 +- .../MVC/View/views/view.multieditTest.php | 4 +- .../MVC/View/views/view.noaccessTest.php | 4 +- .../include/MVC/View/views/view.popupTest.php | 4 +- .../include/MVC/View/views/view.quickTest.php | 4 +- .../MVC/View/views/view.quickcreateTest.php | 4 +- .../MVC/View/views/view.quickeditTest.php | 4 +- .../MVC/View/views/view.serializedTest.php | 4 +- .../MVC/View/views/view.sugarpdfTest.php | 4 +- .../include/MVC/View/views/view.vcardTest.php | 4 +- .../include/MVC/View/views/view.xmlTest.php | 4 +- .../phpunit/include/SubPanel/SubPanelTest.php | 4 +- .../SugarEmailAddressTest.php | 4 +- .../include/SugarFolders/SugarFolderTest.php | 4 +- .../SugarObjects/templates/PersonTest.php | 4 +- .../include/SugarTheme/SugarThemeTest.php | 4 +- tests/unit/phpunit/include/UtilsTest.php | 4 +- .../phpunit/include/utils/LogicHookTest.php | 4 +- .../include/utils/activityUtilsTest.php | 4 +- .../phpunit/include/utils/arrayUtilsTest.php | 4 +- .../phpunit/include/utils/autoloaderTest.php | 4 +- .../phpunit/include/utils/dbUtilsTest.php | 4 +- .../include/utils/encryptionUtilsTest.php | 4 +- .../phpunit/include/utils/fileUtilsTest.php | 4 +- .../phpunit/include/utils/layoutUtilsTest.php | 4 +- .../phpunit/include/utils/logicUtilsTest.php | 4 +- .../phpunit/include/utils/mvcUtilsTest.php | 4 +- .../phpunit/include/utils/phpZipUtilsTest.php | 4 +- .../include/utils/progressBarUtilsTest.php | 4 +- .../include/utils/securityUtilsTest.php | 4 +- .../include/utils/sugarFileUtilsTest.php | 4 +- .../phpunit/include/utils/zipUtilsTest.php | 4 +- .../lib/SuiteCRM/Exception/ExceptionTest.php | 4 +- .../Commands/CodeCoverageCommandsTest.php | 4 +- .../SuiteCRM/Utility/CurrentLanguageTest.php | 4 +- .../SuiteCRM/Utility/ModuleLanguageTest.php | 4 +- .../lib/SuiteCRM/Utility/PathsTest.php | 4 +- .../SuiteCRM/Utility/StringValidatorTest.php | 4 +- .../lib/SuiteCRM/Utility/SuiteLoggerTest.php | 4 +- .../modules/ACLActions/ACLActionTest.php | 55 ++++++++++++++----- .../phpunit/modules/ACLRoles/ACLRoleTest.php | 4 +- .../AM_ProjectTemplatesTest.php | 4 +- .../AM_TaskTemplates/AM_TaskTemplatesTest.php | 4 +- .../modules/AOD_Index/AOD_IndexTest.php | 4 +- .../AOD_IndexEvent/AOD_IndexEventTest.php | 4 +- .../AOK_KnowledgeBaseTest.php | 4 +- .../AOK_Knowledge_Base_CategoriesTest.php | 4 +- .../AOP_Case_Events/AOP_Case_EventsTest.php | 4 +- .../AOP_Case_Updates/AOP_Case_UpdatesTest.php | 4 +- .../modules/AOR_Charts/AOR_ChartTest.php | 4 +- .../AOR_Conditions/AOR_ConditionTest.php | 4 +- .../modules/AOR_Fields/AOR_FieldTest.php | 4 +- .../modules/AOR_Reports/AOR_ReportTest.php | 4 +- .../AOR_Scheduled_ReportsTest.php | 4 +- .../AOS_Contracts/AOS_ContractsTest.php | 4 +- .../modules/AOS_Invoices/AOS_InvoicesTest.php | 4 +- .../AOS_Line_Item_GroupsTest.php | 4 +- .../AOS_PDF_TemplatesTest.php | 4 +- .../AOS_Product_CategoriesTest.php | 4 +- .../modules/AOS_Products/AOS_ProductsTest.php | 4 +- .../AOS_Products_QuotesTest.php | 4 +- .../modules/AOS_Quotes/AOS_QuotesTest.php | 4 +- .../modules/AOW_Actions/AOW_ActionTest.php | 4 +- .../AOW_Conditions/AOW_ConditionTest.php | 4 +- .../AOW_Processed/AOW_ProcessedTest.php | 4 +- .../modules/AOW_WorkFlow/AOW_WorkFlowTest.php | 4 +- .../phpunit/modules/Accounts/AccountTest.php | 4 +- .../Administration/AdministrationTest.php | 4 +- .../unit/phpunit/modules/Alerts/AlertTest.php | 4 +- .../unit/phpunit/modules/Audit/AuditTest.php | 4 +- .../phpunit/modules/Audit/PopupPickerTest.php | 4 +- tests/unit/phpunit/modules/Bugs/BugTest.php | 4 +- tests/unit/phpunit/modules/Calls/CallTest.php | 4 +- .../Calls_Reschedule/Calls_RescheduleTest.php | 4 +- .../modules/CampaignLog/CampaignLogTest.php | 4 +- .../CampaignTrackers/CampaignTrackerTest.php | 4 +- .../modules/Campaigns/CampaignTest.php | 4 +- tests/unit/phpunit/modules/Cases/CaseTest.php | 4 +- .../phpunit/modules/Contacts/ContactTest.php | 4 +- .../modules/Currencies/CurrencyTest.php | 4 +- .../DocumentRevisionTest.php | 4 +- .../modules/Documents/DocumentTest.php | 4 +- tests/unit/phpunit/modules/EAPM/EAPMTest.php | 4 +- .../EmailAddresses/EmailAddressTest.php | 4 +- .../phpunit/modules/EmailMan/EmailManTest.php | 4 +- .../EmailMarketing/EmailMarketingTest.php | 4 +- .../EmailTemplates/EmailTemplateTest.php | 4 +- .../modules/EmailText/EmailTextTest.php | 4 +- .../modules/Emails/EmailFromValidatorTest.php | 4 +- .../unit/phpunit/modules/Emails/EmailTest.php | 4 +- .../Emails/NonGmailSentFolderHandlerTest.php | 4 +- .../modules/Employees/EmployeeTest.php | 4 +- .../FP_Event_LocationsTest.php | 4 +- .../modules/FP_events/FP_eventsTest.php | 4 +- .../modules/Favorites/FavoritesTest.php | 4 +- .../unit/phpunit/modules/Groups/GroupTest.php | 4 +- .../modules/InboundEmail/InboundEmailTest.php | 4 +- tests/unit/phpunit/modules/Leads/LeadTest.php | 4 +- .../phpunit/modules/Meetings/MeetingTest.php | 4 +- .../modules/MergeRecords/MergeRecordTest.php | 4 +- tests/unit/phpunit/modules/Notes/NoteTest.php | 4 +- .../modules/OAuthKeys/OAuthKeyTest.php | 4 +- .../modules/OAuthTokens/OAuthTokenTest.php | 4 +- .../modules/Opportunities/OpportunityTest.php | 4 +- .../phpunit/modules/Project/ProjectTest.php | 4 +- .../modules/ProjectTask/ProjectTaskTest.php | 4 +- .../ProspectLists/ProspectListTest.php | 4 +- .../modules/Prospects/ProspectTest.php | 4 +- .../Relationships/RelationshipTest.php | 4 +- .../phpunit/modules/Releases/ReleaseTest.php | 4 +- tests/unit/phpunit/modules/Roles/RoleTest.php | 4 +- .../modules/SavedSearch/SavedSearchTest.php | 4 +- .../modules/Schedulers/SchedulerTest.php | 4 +- .../SchedulersJobs/SchedulersJobTest.php | 4 +- .../SecurityGroups/SecurityGroupTest.php | 4 +- .../modules/SugarFeed/SugarFeedTest.php | 4 +- tests/unit/phpunit/modules/Tasks/TaskTest.php | 4 +- .../phpunit/modules/Trackers/TrackerTest.php | 4 +- .../UserPreferences/UserPreferenceTest.php | 4 +- tests/unit/phpunit/modules/Users/UserTest.php | 4 +- tests/unit/phpunit/modules/iCals/iCalTest.php | 4 +- .../jjwg_Address_CacheTest.php | 4 +- .../modules/jjwg_Areas/jjwg_AreasTest.php | 4 +- .../modules/jjwg_Maps/jjwg_MapsTest.php | 4 +- .../modules/jjwg_Markers/jjwg_MarkersTest.php | 4 +- tests/unit/phpunit/modules/vCals/vCalTest.php | 4 +- .../service/v4/SugarWebServiceUtilv4Test.php | 4 +- 158 files changed, 392 insertions(+), 345 deletions(-) rename tests/SuiteCRM/Test/{SuitePHPUnit_Framework_TestCase.php => SuitePHPUnitFrameworkTestCase.php} (78%) diff --git a/include/DatabaseTransactions.php b/include/DatabaseTransactions.php index cda26b08262..1a4f34139e7 100644 --- a/include/DatabaseTransactions.php +++ b/include/DatabaseTransactions.php @@ -58,7 +58,8 @@ public function startDBTransaction() $db->query('START TRANSACTION'); } - public function rollbackDBTransaction() { + public function rollbackDBTransaction() + { $db = DBManagerFactory::getInstance(); $db->query('ROLLBACK'); } diff --git a/include/RefreshDatabase.php b/include/RefreshDatabase.php index 9ff6c6392ff..c2ea1c529a7 100644 --- a/include/RefreshDatabase.php +++ b/include/RefreshDatabase.php @@ -54,16 +54,22 @@ trait RefreshDatabase { /** - * Truncates the database before each unit test + * Truncates the database/table before each unit test + * @param string $table database table to truncate or 'ALL' to truncate all tables. * @throws Exception */ - public function refreshDatabase() + public function refreshDatabase($table = 'ALL') { $db = DBManagerFactory::getInstance(); - foreach ($db->getTablesArray() as $table) { - if (!$db->query('TRUNCATE TABLE ' . $table)) { - throw new Exception('Failed to truncate database'); + + if ($table === 'ALL') { + foreach ($db->getTablesArray() as $table) { + if (!$db->query('TRUNCATE TABLE ' . $table)) { + throw new Exception('Failed to truncate database'); + } } + } elseif (!$db->query('TRUNCATE TABLE ' . $table)) { + throw new Exception('Failed to truncate table: ' . $table); } } } diff --git a/include/TestCaseAbstract.php b/include/TestCaseAbstract.php index f1316eed882..0bef8d8a0f1 100644 --- a/include/TestCaseAbstract.php +++ b/include/TestCaseAbstract.php @@ -56,15 +56,20 @@ abstract class TestCaseAbstract extends PHPUnit_Framework_TestCase use DatabaseTransactions; use RefreshDatabase; - protected static $verbose = true; - protected static $cleanupStrategy = 'transaction'; + protected $verbose; + protected $cleanupStrategy; /** + * @param bool $verbose + * @param string $cleanupStrategy * @throws Exception */ - protected function setUp() + protected function setUp($verbose = true, $cleanupStrategy = 'transaction') { - if (self::$verbose) { + $this->verbose = $verbose; + $this->cleanupStrategy = $cleanupStrategy; + + if ($this->verbose) { $currentTestName = get_class($this) . '::' . $this->getName(false); fwrite(STDOUT, "\t" . $currentTestName . ' ..'); for ($i = 60, $iMax = strlen($currentTestName); $i > $iMax; $i--) { @@ -72,9 +77,9 @@ protected function setUp() } } - if (self::$cleanupStrategy === 'transaction') { + if ($this->cleanupStrategy === 'transaction') { $this->startDBTransaction(); - } elseif (self::$cleanupStrategy === 'refresh') { + } elseif ($this->cleanupStrategy === 'refresh') { $this->refreshDatabase(); } else { throw new Exception('Failed to truncate database'); @@ -86,11 +91,11 @@ protected function tearDown() { parent::tearDown(); - if (self::$cleanupStrategy === 'transaction') { + if ($this->cleanupStrategy === 'transaction') { $this->rollbackDBTransaction(); } - if (self::$verbose) { + if ($this->verbose) { fwrite(STDOUT, " [done]\n"); } } diff --git a/tests/SuiteCRM/Test/SuitePHPUnit_Framework_TestCase.php b/tests/SuiteCRM/Test/SuitePHPUnitFrameworkTestCase.php similarity index 78% rename from tests/SuiteCRM/Test/SuitePHPUnit_Framework_TestCase.php rename to tests/SuiteCRM/Test/SuitePHPUnitFrameworkTestCase.php index 436bfb698fc..dbe4c614b0c 100644 --- a/tests/SuiteCRM/Test/SuitePHPUnit_Framework_TestCase.php +++ b/tests/SuiteCRM/Test/SuitePHPUnitFrameworkTestCase.php @@ -1,11 +1,19 @@ log = $GLOBALS['log']; @@ -94,7 +102,7 @@ public function tearDown() $GLOBALS['log'] = $this->log; DBManagerFactory::$instances = $this->dbManagerFactoryInstances; - + parent::tearDown(); } } diff --git a/tests/unit/phpunit/Api/Core/Loader/CustomLoaderTest.php b/tests/unit/phpunit/Api/Core/Loader/CustomLoaderTest.php index 609addd04dd..9dd9e6fb7f0 100644 --- a/tests/unit/phpunit/Api/Core/Loader/CustomLoaderTest.php +++ b/tests/unit/phpunit/Api/Core/Loader/CustomLoaderTest.php @@ -40,14 +40,14 @@ use Api\Core\Loader\CustomLoader; use Slim\App; -use SuiteCRM\Test\SuitePHPUnit_Framework_TestCase; +use SuiteCRM\Test\SuitePHPUnitFrameworkTestCase; /** * CustomLoaderTest * * @author gyula */ -class CustomLoaderTest extends SuitePHPUnit_Framework_TestCase +class CustomLoaderTest extends SuitePHPUnitFrameworkTestCase { public function testArrayMerge() { diff --git a/tests/unit/phpunit/configTest.php b/tests/unit/phpunit/configTest.php index dd6d9269726..0e84432b2aa 100644 --- a/tests/unit/phpunit/configTest.php +++ b/tests/unit/phpunit/configTest.php @@ -1,8 +1,8 @@ assertFalse(ACLAction::userNeedsSecurityGroup('1', '', ''));//test with empty module and action - $this->assertFalse(ACLAction::userNeedsSecurityGroup('1', 'Accounts', 'list')); //test with valid module and action + $this->assertFalse(ACLAction::userNeedsSecurityGroup('1', 'Accounts', + 'list')); //test with valid module and action } public function testuserHasAccess() { self::markTestIncomplete('Need to fix checking user access. Hint: session is a system state perhaps its failing because the user session'); - + $this->assertFalse(ACLAction::userHasAccess('', '', '')); //test with empty module and action - $this->assertTrue(ACLAction::userHasAccess('', 'Accounts', 'list')); //test with e,pty user and valid module and action + $this->assertTrue(ACLAction::userHasAccess('', 'Accounts', + 'list')); //test with e,pty user and valid module and action $this->assertTrue(ACLAction::userHasAccess('1', 'Accounts', 'list')); //test with valid User, module and action - $this->assertTrue(ACLAction::userHasAccess('1', 'SecurityGroups', 'list')); //test with valid User, module and action + $this->assertTrue(ACLAction::userHasAccess('1', 'SecurityGroups', + 'list')); //test with valid User, module and action $this->assertTrue(ACLAction::userHasAccess('1', 'Users', 'list')); //test with valid User, module and action } @@ -134,7 +137,7 @@ public function testgetUserAccessLevel() public function testuserNeedsOwnership() { self::markTestIncomplete('Need to fix checking user access. Hint: session is a system state perhaps its failing because the user session'); - + //test with invalid values $this->assertFalse(ACLAction::userNeedsOwnership('', '', '')); @@ -156,12 +159,36 @@ public function testsetupCategoriesMatrix() $names_expected = array('list' => 'List', 'edit' => 'Edit'); $categories_expected = array( - 'Accounts' => array( - 'module' => array( - 'list' => array('list', 'accessColor' => false, 'accessName' => false, 'accessLabel' => false, 'accessOptions' => array(90 => 'All', 80 => 'Group', 75 => 'Owner', 0 => 'Not Set', -99 => 'None')), - 'edit' => array('edit', 'accessColor' => false, 'accessName' => false, 'accessLabel' => false, 'accessOptions' => array(90 => 'All', 80 => 'Group', 75 => 'Owner', 0 => 'Not Set', -99 => 'None')), - ), + 'Accounts' => array( + 'module' => array( + 'list' => array( + 'list', + 'accessColor' => false, + 'accessName' => false, + 'accessLabel' => false, + 'accessOptions' => array( + 90 => 'All', + 80 => 'Group', + 75 => 'Owner', + 0 => 'Not Set', + -99 => 'None' + ) + ), + 'edit' => array( + 'edit', + 'accessColor' => false, + 'accessName' => false, + 'accessLabel' => false, + 'accessOptions' => array( + 90 => 'All', + 80 => 'Group', + 75 => 'Owner', + 0 => 'Not Set', + -99 => 'None' + ) + ), ), + ), ); //execute the method and verify that it retunrs expected results diff --git a/tests/unit/phpunit/modules/ACLRoles/ACLRoleTest.php b/tests/unit/phpunit/modules/ACLRoles/ACLRoleTest.php index 9544ed8b177..bad9147fe0f 100644 --- a/tests/unit/phpunit/modules/ACLRoles/ACLRoleTest.php +++ b/tests/unit/phpunit/modules/ACLRoles/ACLRoleTest.php @@ -1,8 +1,8 @@