Merge pull request #17323 from JKingsnorth/patch-14
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / SettingTest.php
index c477fd041158abf9fa549fd7e07e80ac24c41927..34cf4a8f47d7e286eb3c4aa6c3ed6db4bc740286 100644 (file)
  */
 class CRM_Core_BAO_SettingTest extends CiviUnitTestCase {
 
+  /**
+   * Original value of civicrm_setting global.
+   * @var array
+   */
+  private $origSetting;
+
   public function setUp() {
     parent::setUp();
     global $civicrm_setting;
     $this->origSetting = $civicrm_setting;
-    CRM_Utils_Cache::singleton()->flush();
+    CRM_Utils_Cache::singleton()->clear();
   }
 
+  /**
+   * Clean up after test.
+   *
+   * @throws \CRM_Core_Exception
+   */
   public function tearDown() {
     global $civicrm_setting;
     $civicrm_setting = $this->origSetting;
-    CRM_Utils_Cache::singleton()->flush();
+    $this->quickCleanup(['civicrm_contribution']);
+    CRM_Utils_Cache::singleton()->clear();
     parent::tearDown();
   }
 
+  /**
+   * Test that enabling a valid component works.
+   */
   public function testEnableComponentValid() {
-    $config = CRM_Core_Config::singleton(TRUE, TRUE);
-
+    CRM_Core_Config::singleton(TRUE, TRUE);
     $result = CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign');
-
     $this->assertTrue($result);
   }
 
+  /**
+   * Test that we get a success result if we try to enable an enabled component.
+   */
   public function testEnableComponentAlreadyPresent() {
-    $config = CRM_Core_Config::singleton(TRUE, TRUE);
-
-    $result = CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign');
+    CRM_Core_Config::singleton(TRUE, TRUE);
+    CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign');
     $result = CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign');
-
     $this->assertTrue($result);
   }
 
+  /**
+   * Test that we get a false result if we try to enable an invalid component.
+   */
   public function testEnableComponentInvalid() {
-    $config = CRM_Core_Config::singleton(TRUE, TRUE);
-
+    CRM_Core_Config::singleton(TRUE, TRUE);
     $result = CRM_Core_BAO_ConfigSetting::enableComponent('CiviFake');
-
     $this->assertFalse($result);
   }
 
+  /**
+   * Test temporary retrieval & setting of converted settings.
+   *
+   * As a transitional measure we allow the settings that were munged into
+   * contribution_invoice_setting. This tests that the current method of getting via the 'old' key
+   * works. This will be deprecated & removed over the next few versions but
+   * 1) some extensions use these settings &
+   * 2) there is a lot of work to fix this mess in core so a transitional method makes sense.
+   *
+   * https://lab.civicrm.org/dev/core/issues/1558
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public function testHandlingOfContributionInvoiceSetting() {
+    $contributionSettings = [
+      'invoice_prefix' => 'G_',
+      'credit_notes_prefix' => 'XX_',
+      'due_date' => '20',
+      'due_date_period' => 'weeks',
+      'notes' => '<p>Give me money</p>',
+      'tax_term' => 'Extortion',
+      'tax_display_settings' => 'Exclusive',
+      // NOTE: This form of `invoicing` is accepted, but it may be normalized to the idiomatic form with a nested array.
+      'invoicing' => 1,
+      'is_email_pdf' => '1',
+    ];
+    Civi::settings()->set('contribution_invoice_settings', $contributionSettings);
+    $settingsFromGet = Civi::settings()->get('contribution_invoice_settings');
+    $settingsFromAPI = $this->callAPISuccess('Setting', 'get', ['return' => 'contribution_invoice_settings'])['values'][CRM_Core_Config::domainID()]['contribution_invoice_settings'];
+    $getVersion = $this->callAPISuccessGetValue('Setting', ['name' => 'contribution_invoice_settings']);
+    $this->assertEquals($settingsFromAPI, $settingsFromGet);
+    $this->assertAPIArrayComparison($getVersion, $settingsFromGet);
+    $this->assertEquals(['invoicing' => ['invoicing' => 1]] + $contributionSettings, $settingsFromGet);
+
+    // These are the preferred retrieval methods.
+    $this->assertEquals('G_', Civi::settings()->get('invoice_prefix'));
+    $this->assertEquals('XX_', Civi::settings()->get('credit_notes_prefix'));
+    $this->assertEquals('20', Civi::settings()->get('invoice_due_date'));
+    $this->assertEquals('weeks', Civi::settings()->get('invoice_due_date_period'));
+    $this->assertEquals('<p>Give me money</p>', Civi::settings()->get('invoice_notes'));
+    $this->assertEquals('Extortion', Civi::settings()->get('tax_term'));
+    $this->assertEquals('Exclusive', Civi::settings()->get('tax_display_settings'));
+  }
+
   /**
    * Ensure that overrides in $civicrm_setting apply when
    * using getItem($group,$name).
@@ -181,4 +240,13 @@ class CRM_Core_BAO_SettingTest extends CiviUnitTestCase {
     $this->assertEquals('Development', $environment);
   }
 
+  /**
+   * Test that options defined as a pseudoconstant can be converted to options.
+   */
+  public function testPseudoConstants() {
+    $this->contributionPageCreate();
+    $metadata = \Civi\Core\SettingsMetadata::getMetadata(['name' => ['default_invoice_page']], NULL, TRUE);
+    $this->assertEquals('Test Contribution Page', $metadata['default_invoice_page']['options'][1]);
+  }
+
 }