Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM-18251 - Domain stats and VersionCheck #8525

Merged
merged 1 commit into from
Mar 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions CRM/Utils/VersionCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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'];
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we explicitly:
} else {
$this->stats['domain_country_iso'] = NULL; (or '';)
}
??

}

/**
* Send the request to civicrm.org
* Store results in the cache file
Expand Down
97 changes: 97 additions & 0 deletions tests/phpunit/CRM/Utils/versionCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}

}