diff --git a/CRM/Utils/VersionCheck.php b/CRM/Utils/VersionCheck.php index 364c8466110a..6cd1507bd69c 100644 --- a/CRM/Utils/VersionCheck.php +++ b/CRM/Utils/VersionCheck.php @@ -287,6 +287,7 @@ private function getSiteStats() { 'MySQL' => CRM_CORE_DAO::singleValueQuery('SELECT VERSION()'), 'communityMessagesUrl' => Civi::settings()->get('communityMessagesUrl'), ); + $this->getDomainStats(); $this->getPayProcStats(); $this->getEntityStats(); $this->getExtensionStats(); @@ -339,6 +340,7 @@ private function getEntityStats() { 'CRM_Member_DAO_MembershipBlock' => 'is_active = 1', 'CRM_Pledge_DAO_Pledge' => 'is_test = 0', 'CRM_Pledge_DAO_PledgeBlock' => NULL, + 'CRM_Mailing_Event_DAO_Delivered' => NULL, ); foreach ($tables as $daoName => $where) { $dao = new $daoName(); @@ -381,6 +383,36 @@ private function getExtensionStats() { } } + /** + * Fetch stats about domain and add to 'stats' array. + */ + private function getDomainStats() { + // Start with default value NULL, then check to see if there's a better + // value to be had. + $this->stats['domain_isoCode'] = NULL; + $params = array( + 'id' => CRM_Core_Config::domainID(), + ); + $domain_result = civicrm_api3('domain', 'getsingle', $params); + if (!empty($domain_result['contact_id'])) { + $address_params = array( + 'contact_id' => $domain_result['contact_id'], + 'is_primary' => 1, + 'sequential' => 1, + ); + $address_result = civicrm_api3('address', 'get', $address_params); + if ($address_result['count'] == 1 && !empty($address_result['values'][0]['country_id'])) { + $country_params = array( + 'id' => $address_result['values'][0]['country_id'], + ); + $country_result = civicrm_api3('country', 'getsingle', $country_params); + if (!empty($country_result['iso_code'])) { + $this->stats['domain_isoCode'] = $country_result['iso_code']; + } + } + } + } + /** * Send the request to civicrm.org * Store results in the cache file diff --git a/tests/phpunit/CRM/Utils/versionCheckTest.php b/tests/phpunit/CRM/Utils/versionCheckTest.php index 5caa80135acb..4c0322d93cf1 100644 --- a/tests/phpunit/CRM/Utils/versionCheckTest.php +++ b/tests/phpunit/CRM/Utils/versionCheckTest.php @@ -233,4 +233,101 @@ public function testCronFallback() { $this->assertEquals($remoteData, $vc->versionInfo); } + public function testGetSiteStats() { + // Create domain address so the domain country will come up in the stats. + $country_params = array( + 'sequential' => 1, + 'options' => array( + 'limit' => 1, + ), + ); + $country_result = civicrm_api3('country', 'get', $country_params); + $country = $country_result['values'][0]; + + $domain_params = array( + 'id' => CRM_Core_Config::domainID(), + ); + CRM_Core_BAO_Domain::retrieve($domain_params, $domain_defaults); + $location_type = CRM_Core_BAO_LocationType::getDefault(); + $address_params = array( + 'contact_id' => $domain_defaults['contact_id'], + 'location_type_id' => $location_type->id, + 'is_primary' => '1', + 'is_billing' => '0', + 'street_address' => '1 Main St.', + 'city' => 'Anywhere', + 'postal_code' => '99999', + 'country_id' => $country['id'], + ); + $address_result = civicrm_api3('address', 'create', $address_params); + + // Build stats and test them. + $vc = new ReflectionClass('CRM_Utils_VersionCheck'); + $vc_instance = $vc->newInstance(); + + $statsBuilder = $vc->getMethod('getSiteStats'); + $statsBuilder->setAccessible(TRUE); + $statsBuilder->invoke($vc_instance, NULL); + + $statsProperty = $vc->getProperty('stats'); + $statsProperty->setAccessible(TRUE); + $stats = $statsProperty->getValue($vc_instance); + + // Stats array should have correct elements. + $this->assertArrayHasKey('version', $stats); + $this->assertArrayHasKey('hash', $stats); + $this->assertArrayHasKey('uf', $stats); + $this->assertArrayHasKey('lang', $stats); + $this->assertArrayHasKey('co', $stats); + $this->assertArrayHasKey('ufv', $stats); + $this->assertArrayHasKey('PHP', $stats); + $this->assertArrayHasKey('MySQL', $stats); + $this->assertArrayHasKey('communityMessagesUrl', $stats); + $this->assertArrayHasKey('domain_isoCode', $stats); + $this->assertArrayHasKey('PPTypes', $stats); + $this->assertArrayHasKey('entities', $stats); + $this->assertArrayHasKey('extensions', $stats); + $this->assertType('array', $stats['entities']); + $this->assertType('array', $stats['extensions']); + + // Assert $stats['domain_isoCode'] is correct. + $this->assertEquals($country['iso_code'], $stats['domain_isoCode']); + + $entity_names = array(); + foreach ($stats['entities'] as $entity) { + $entity_names[] = $entity['name']; + $this->assertType('int', $entity['size'], "Stats entity {$entity['name']} has integer size?"); + } + + $expected_entity_names = array( + 'Activity', + 'Case', + 'Contact', + 'Relationship', + 'Campaign', + 'Contribution', + 'ContributionPage', + 'ContributionProduct', + 'Widget', + 'Discount', + 'PriceSetEntity', + 'UFGroup', + 'Event', + 'Participant', + 'Friend', + 'Grant', + 'Mailing', + 'Membership', + 'MembershipBlock', + 'Pledge', + 'PledgeBlock', + 'Delivered', + ); + sort($entity_names); + sort($expected_entity_names); + $this->assertEquals($expected_entity_names, $entity_names); + + // TODO: Also test for enabled extensions. + } + }