Add first test, and fix the bugs it found!

This commit is contained in:
Rich Lott / Artful Robot 2025-03-25 22:02:52 +00:00
parent 92db3be27e
commit 78e5b83c1d
3 changed files with 183 additions and 21 deletions

View file

@ -0,0 +1,137 @@
<?php
namespace Civi\Api4\Action\ContactCategory;
use Civi\Api4\Contact;
use Civi\Api4\Activity;
use Civi\Api4\ContactCategory;
use Civi\Api4\ContactCategoryDefinition;
use Civi\Api4\Group;
use CRM_Contactcats_ExtensionUtil as E;
use Civi\Test\CiviEnvBuilder;
use Civi\Test\HeadlessInterface;
use Civi\Test\HookInterface;
use Civi\Test\TransactionalInterface;
/**
* Check our stats are predicatable.
*
* Tips:
* - With HookInterface, you may implement CiviCRM hooks directly in the test class.
* Simply create corresponding functions (e.g. "hook_civicrm_post(...)" or similar).
* - With TransactionalInterface, any data changes made by setUp() or test****() functions will
* rollback automatically -- as long as you don't manipulate schema or truncate tables.
* If this test needs to manipulate schema or truncate tables, then either:
* a. Do all that using setupHeadless() and Civi\Test.
* b. Disable TransactionalInterface, and handle all setup/teardown yourself.
*
* @method void assertNotEmpty()
* @method void assertEquals()
* @method void assertMatchesRegularExpression()
* @group headless
*/
class GetFlowsTest extends \PHPUnit\Framework\TestCase implements HeadlessInterface, HookInterface, TransactionalInterface {
/**
* Setup used when HeadlessInterface is implemented.
*
* Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile().
*
* @link https://github.com/civicrm/org.civicrm.testapalooza/blob/master/civi-test.md
*
* @return \Civi\Test\CiviEnvBuilder
*
* @throws \CRM_Extension_Exception_ParseException
*/
public function setUpHeadless(): CiviEnvBuilder {
return \Civi\Test::headless()
->installMe(__DIR__)
->apply();
}
public function setUp():void {
parent::setUp();
}
public function tearDown():void {
parent::tearDown();
}
/**
* Example: Test that a version is returned.
*/
// public function testWellFormedVersion():void {
// $this->assertNotEmpty(E::SHORT_NAME);
// $this->assertMatchesRegularExpression('/^([0-9\.]|alpha|beta)*$/', \CRM_Utils_System::version());
/**
* }
*/
public function testGetFlows():void {
// Create groups for our cats.
[$amazingGroupID, $mehGroupID] = Group::save(FALSE)
->setRecords([
['name' => 'amazing', 'frontend_title' => 'amazing'],
['name' => 'meh', 'frontend_title' => 'meh'],
])->execute()->column('id');
// Create cats.
[$amazingCatID, $mehCatID] = ContactCategoryDefinition::save(FALSE)
->setRecords([
[
'label' => 'Amazing',
'search_type:name' => 'group',
'search_data' => ['group_id' => $amazingGroupID],
'presentation_order' => 1,
'execution_order' => 1,
'color' => '#ff0000',
],
[
'label' => 'Meh',
'search_type:name' => 'group',
'search_data' => ['group_id' => $mehGroupID],
'presentation_order' => 2,
'execution_order' => 2,
'color' => '#fff000',
],
])
->execute()->column('id');
// Create a contact
$ctID = Contact::create(FALSE)
->setValues([
'contact_type' => 'Individual',
'display_name' => 'Wilma',
])->execute()->first()['id'];
// print "xxxxx created ct $ctID\n";
// Create two activities.
$activityIDs = Activity::save(FALSE)
->setDefaults([
'activity_type_id:name' => 'changed_contact_category',
'target_contact_id' => $ctID,
'source_contact_id' => $ctID,
])
->setRecords([
[
'activity_date_time' => '2025-01-01',
'Category_changes.new_category_id' => $amazingCatID,
],
[
'activity_date_time' => '2025-02-01',
'Category_changes.new_category_id' => $mehCatID,
],
])->execute()->column('id');
// $acts = Activity::get(FALSE)->addWhere('id', 'IN', $activityIDs)->execute()->getArrayCopy(); print_r($acts);
// FIXME: this is returning backwards.
$flows = ContactCategory::getFlows()
->setStartDate('2025-01-03')
->execute()->getArrayCopy();
// print_r($flows);
$this->assertEquals([
['from_category_id' => $amazingCatID, 'to_category_id' => $mehCatID, 'contact_count' => 1],
], $flows);
}
}