<?xml version="1.0"?>
-<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
- backupGlobals="false"
- backupStaticAttributes="false"
- colors="true"
- convertErrorsToExceptions="true"
- convertNoticesToExceptions="true"
- convertWarningsToExceptions="true"
- convertDeprecationsToExceptions="true"
- processIsolation="false"
- stopOnFailure="false"
- bootstrap="tests/phpunit/bootstrap.php"
- cacheResult="false">
- <coverage>
- <include>
- <directory suffix=".php">./</directory>
- </include>
- </coverage>
+<phpunit backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" convertDeprecationsToExceptions="true" processIsolation="false" stopOnFailure="false" cacheResult="false" bootstrap="tests/phpunit/bootstrap.php">
<testsuites>
<testsuite name="My Test Suite">
<directory>./tests/phpunit</directory>
</testsuite>
- <testsuite name="form-tests">
- <directory suffix=".test.php">./ang</directory>
- </testsuite>
</testsuites>
+ <filter>
+ <whitelist>
+ <directory suffix=".php">./</directory>
+ </whitelist>
+ </filter>
<listeners>
<listener class="Civi\Test\CiviTestListener">
<arguments/>
<?php
+namespace api\v4\Afform;
use Civi\Api4\Afform;
use Civi\Api4\Contact;
*
* @group headless
*/
-class api_v4_AfformAutocompleteUsageTest extends api_v4_AfformUsageTestCase {
+class AfformAutocompleteUsageTest extends AfformUsageTestCase {
/**
* Ensure that Afform restricts autocomplete results when it's set to use a SavedSearch
$this->useValues([
'layout' => $layout,
- 'permission' => CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
+ 'permission' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
]);
// Saved search for filtering
->execute();
$this->fail();
}
- catch (CRM_Core_Exception $e) {
+ catch (\CRM_Core_Exception $e) {
$this->assertEquals('Validation Error', $e->getMessage());
}
$this->useValues([
'layout' => $layout,
- 'permission' => CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
+ 'permission' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
]);
$result = Contact::autocomplete()
->execute();
$this->fail();
}
- catch (CRM_Core_Exception $e) {
+ catch (\CRM_Core_Exception $e) {
$this->assertEquals('Validation Error', $e->getMessage());
}
$this->useValues([
'layout' => $layout,
- 'permission' => CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
+ 'permission' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
]);
$result = Contact::autocomplete()
->execute();
$this->fail();
}
- catch (CRM_Core_Exception $e) {
+ catch (\CRM_Core_Exception $e) {
$this->assertEquals('Validation Error', $e->getMessage());
}
<?php
+namespace api\v4\Afform;
use Civi\Api4\Afform;
+use Civi\Api4\AfformSubmission;
+use Civi\Api4\Contact;
/**
* Test case for Afform.checkAccess, Afform.prefill and Afform.submit.
*
* @group headless
*/
-class api_v4_AfformContactUsageTest extends api_v4_AfformUsageTestCase {
+class AfformContactUsageTest extends AfformUsageTestCase {
public static function setUpBeforeClass(): void {
parent::setUpBeforeClass();
public function testAboutMeAllowed(): void {
$this->useValues([
'layout' => self::$layouts['aboutMe'],
- 'permission' => CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
+ 'permission' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
]);
$cid = $this->createLoggedInUser();
- CRM_Core_Config::singleton()->userPermissionTemp = new CRM_Core_Permission_Temp();
+ \CRM_Core_Config::singleton()->userPermissionTemp = new \CRM_Core_Permission_Temp();
// Autofill form with current user. See `Civi\Afform\Behavior\ContactAutofill`
$prefill = Afform::prefill()
->setValues(['me' => $submission])
->execute();
- $contact = Civi\Api4\Contact::get(FALSE)->addWhere('id', '=', $cid)->execute()->first();
+ $contact = Contact::get(FALSE)->addWhere('id', '=', $cid)->execute()->first();
$this->assertEquals('Firsty', $contact['first_name']);
$this->assertEquals('Lasty', $contact['last_name']);
}
public function testChainSelect(): void {
$this->useValues([
'layout' => self::$layouts['aboutMe'],
- 'permission' => CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
+ 'permission' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
]);
// Get states for USA
->setModelName('me')
->setFieldName('state_province_id')
->setJoinEntity('Address')
- ->setValues(['country_id' => CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Country', 'United States', 'id', 'name')])
+ ->setValues(['country_id' => \CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Country', 'United States', 'id', 'name')])
->execute();
$this->assertEquals('Alabama', $result[0]['label']);
->setModelName('me')
->setFieldName('state_province_id')
->setJoinEntity('Address')
- ->setValues(['country_id' => CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Country', 'United Kingdom', 'id', 'name')])
+ ->setValues(['country_id' => \CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Country', 'United Kingdom', 'id', 'name')])
->execute();
$this->assertEquals('Aberdeen City', $result[0]['label']);
}
public function testCheckEntityReferenceFieldsReplacement(): void {
$this->useValues([
'layout' => self::$layouts['registerSite'],
- 'permission' => CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
+ 'permission' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
'create_submission' => TRUE,
]);
->setValues($values)
->execute();
- $submission = Civi\Api4\AfformSubmission::get(FALSE)
+ $submission = AfformSubmission::get(FALSE)
->addOrderBy('id', 'DESC')
->execute()->first();
'permission' => ['access CiviCRM'],
]);
$this->createLoggedInUser();
- CRM_Core_Config::singleton()->userPermissionTemp = NULL;
- CRM_Core_Config::singleton()->userPermissionClass->permissions = [
+ \CRM_Core_Config::singleton()->userPermissionTemp = NULL;
+ \CRM_Core_Config::singleton()->userPermissionClass->permissions = [
'access Contact Dashboard',
];
$check = Afform::checkAccess()
->setAction('get')
->execute()->first();
$this->assertFalse($check['access']);
- CRM_Core_Config::singleton()->userPermissionClass->permissions = [
+ \CRM_Core_Config::singleton()->userPermissionClass->permissions = [
'access CiviCRM',
];
$check = Afform::checkAccess()
public function testAboutMeForbidden(): void {
$this->useValues([
'layout' => self::$layouts['aboutMe'],
- 'permission' => CRM_Core_Permission::ALWAYS_DENY_PERMISSION,
+ 'permission' => \CRM_Core_Permission::ALWAYS_DENY_PERMISSION,
]);
$this->createLoggedInUser();
- CRM_Core_Config::singleton()->userPermissionTemp = new CRM_Core_Permission_Temp();
+ \CRM_Core_Config::singleton()->userPermissionTemp = new \CRM_Core_Permission_Temp();
try {
Afform::prefill()
public function testEmployerReference(): void {
$this->useValues([
'layout' => self::$layouts['employer'],
- 'permission' => CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
+ 'permission' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
]);
$firstName = uniqid(__FUNCTION__);
public function testEmptyEmployerReference(): void {
$this->useValues([
'layout' => self::$layouts['employer'],
- 'permission' => CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
+ 'permission' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
]);
$firstName = uniqid(__FUNCTION__);
$this->useValues([
'layout' => self::$layouts['employer'],
'create_submission' => TRUE,
- 'permission' => CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
+ 'permission' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
]);
$individualEmail = uniqid('individual@') . '.test';
$orgEmail = uniqid('org@') . '.test';
- $locationType = CRM_Core_BAO_LocationType::getDefault()->id;
+ $locationType = \CRM_Core_BAO_LocationType::getDefault()->id;
$values = [
'Individual1' => [
[
EOHTML;
$this->useValues([
'layout' => $layout,
- 'permission' => CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
+ 'permission' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
]);
$lastName = uniqid(__FUNCTION__);
->addValue('email_primary.email', '123@example.com')
->execute()->single();
- $locationType = CRM_Core_BAO_LocationType::getDefault()->id;
+ $locationType = \CRM_Core_BAO_LocationType::getDefault()->id;
$values = [
'Individual1' => [
[
public function testFormValidationEntityFields(): void {
$this->useValues([
'layout' => self::$layouts['updateInfo'],
- 'permission' => CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
+ 'permission' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
]);
$values = [
public function testFormValidationEntityJoinFields(): void {
$this->useValues([
'layout' => self::$layouts['updateInfo'],
- 'permission' => CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
+ 'permission' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
]);
$values = [
public function testSubmissionLimit() {
$this->useValues([
'layout' => self::$layouts['aboutMe'],
- 'permission' => CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
+ 'permission' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
'create_submission' => TRUE,
'submit_limit' => 3,
]);
$cid = $this->createLoggedInUser();
- CRM_Core_Config::singleton()->userPermissionTemp = new CRM_Core_Permission_Temp();
+ \CRM_Core_Config::singleton()->userPermissionTemp = new \CRM_Core_Permission_Temp();
$submitValues = [
['fields' => ['first_name' => 'Firsty', 'last_name' => 'Lasty']],
<?php
+namespace api\v4\Afform;
use Civi\Api4\Afform;
use Civi\Api4\Contact;
*
* @group headless
*/
-class api_v4_AfformCustomFieldUsageTest extends api_v4_AfformUsageTestCase {
+class AfformCustomFieldUsageTest extends AfformUsageTestCase {
public static function setUpBeforeClass(): void {
parent::setUpBeforeClass();
$this->useValues([
'layout' => self::$layouts['customMulti'],
- 'permission' => CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
+ 'permission' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
]);
$firstName = uniqid(__FUNCTION__);
$values = [
<?php
+namespace api\v4\Afform;
/**
* Test case for Afform.prefill and Afform.submit.
require_once __DIR__ . '/AfformTestCase.php';
require_once __DIR__ . '/AfformUsageTestCase.php';
-class api_v4_AfformFileUploadTest extends api_v4_AfformUsageTestCase {
+class AfformFileUploadTest extends AfformUsageTestCase {
public static function setUpBeforeClass(): void {
parent::setUpBeforeClass();
$this->useValues([
'layout' => self::$layouts['customFiles'],
- 'permission' => CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
+ 'permission' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
]);
$lastName = uniqid(__FUNCTION__);
<?php
+namespace api\v4\Afform;
use Civi\Api4\Afform;
*
* @group headless
*/
-class api_v4_AfformPrefillUsageTest extends api_v4_AfformUsageTestCase {
+class AfformPrefillUsageTest extends AfformUsageTestCase {
use \Civi\Test\Api4TestTrait;
/**
$this->useValues([
'layout' => $layout,
- 'permission' => CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
+ 'permission' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
]);
$cid = $this->saveTestRecords('Contact', [
$this->useValues([
'layout' => $layout,
- 'permission' => CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
+ 'permission' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
]);
$uid = $this->createLoggedInUser();
<?php
+namespace api\v4\Afform;
use Civi\Api4\Afform;
use Civi\Api4\Contact;
*
* @group headless
*/
-class api_v4_AfformRelationshipUsageTest extends api_v4_AfformUsageTestCase {
+class AfformRelationshipUsageTest extends AfformUsageTestCase {
/**
* Tests creating a relationship between multiple contacts
$this->useValues([
'layout' => $layout,
- 'permission' => CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
+ 'permission' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
]);
$lastName = uniqid(__FUNCTION__);
$this->useValues([
'layout' => $layout,
- 'permission' => CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
+ 'permission' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
]);
$types = [
$this->useValues([
'layout' => $layout,
- 'permission' => CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
+ 'permission' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
]);
$contact = Contact::save(FALSE)
<?php
+namespace api\v4\Afform;
use Civi\Api4\Afform;
* Ensure that the routes created by Afform are working.
* @group e2e
*/
-class api_v4_AfformRoutingTest extends \PHPUnit\Framework\TestCase implements \Civi\Test\EndToEndInterface {
+class AfformRoutingTest extends \PHPUnit\Framework\TestCase implements \Civi\Test\EndToEndInterface {
protected $formName = 'mockPage';
public function testChangingPermissions(): void {
$http = new \GuzzleHttp\Client(['http_errors' => FALSE]);
$url = function ($path, $query = NULL) {
- return CRM_Utils_System::url($path, $query, TRUE, NULL, FALSE);
+ return \CRM_Utils_System::url($path, $query, TRUE, NULL, FALSE);
};
$result = $http->get($url('civicrm/mock-page'));
Afform::update()
->setCheckPermissions(FALSE)
->addWhere('name', '=', $this->formName)
- ->addValue('permission', CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION)
+ ->addValue('permission', \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION)
->execute();
$result = $http->get($url('civicrm/mock-page'));
public function testChangingPath(): void {
$http = new \GuzzleHttp\Client(['http_errors' => FALSE]);
$url = function ($path, $query = NULL) {
- return CRM_Utils_System::url($path, $query, TRUE, NULL, FALSE);
+ return \CRM_Utils_System::url($path, $query, TRUE, NULL, FALSE);
};
Afform::update()
->setCheckPermissions(FALSE)
->addWhere('name', '=', $this->formName)
- ->addValue('permission', CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION)
+ ->addValue('permission', \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION)
->execute();
$this->assertOpensPage($http->get($url('civicrm/mock-page')), 'mock-page');
}
/**
- * @param $result
+ * @param \Psr\Http\Message\ResponseInterface $result
* @param string $directive
*/
- private function assertNotAuthorized(Psr\Http\Message\ResponseInterface $result, $directive) {
+ private function assertNotAuthorized(\Psr\Http\Message\ResponseInterface $result, string $directive) {
$contents = $result->getBody()->getContents();
$this->assertEquals(403, $result->getStatusCode());
$this->assertMatchesRegularExpression(';You are not authorized to access;', $contents);
}
/**
- * @param $result
+ * @param \Psr\Http\Message\ResponseInterface $result
* @param string $directive
* The name of the directive which auto-opens.
*/
- private function assertOpensPage(Psr\Http\Message\ResponseInterface $result, $directive) {
+ private function assertOpensPage(\Psr\Http\Message\ResponseInterface $result, string $directive) {
$contents = $result->getBody()->getContents();
$this->assertEquals(200, $result->getStatusCode());
$this->assertDoesNotMatchRegularExpression(';You are not authorized to access;', $contents);
<?php
+namespace api\v4\Afform;
use Civi\Api4\Afform;
use Civi\Api4\Dashboard;
* This is a generic test class implemented with PHPUnit.
* @group headless
*/
-class api_v4_AfformTest extends api_v4_AfformTestCase {
+class AfformTest extends AfformTestCase {
use \Civi\Test\Api3TestTrait;
use \Civi\Test\ContactTestTrait;
public function getFormatExamples() {
$ex = [];
$formats = ['html', 'shallow', 'deep'];
- foreach (glob(__DIR__ . '/formatExamples/*.php') as $exampleFile) {
+ foreach (glob(__DIR__ . '/../formatExamples/*.php') as $exampleFile) {
$example = require $exampleFile;
if (isset($example['deep'])) {
foreach ($formats as $updateFormat) {
public function getWhitespaceExamples() {
$ex = [];
- foreach (glob(__DIR__ . '/formatExamples/*.php') as $exampleFile) {
+ foreach (glob(__DIR__ . '/../formatExamples/*.php') as $exampleFile) {
$example = require $exampleFile;
if (isset($example['pretty'])) {
$ex[] = ['mockBareFile', $example, $exampleFile];
// The default mockPage has 1 explicit requirement + 2 automatic requirements.
Afform::revert()->addWhere('name', '=', $formName)->execute();
- $angModule = Civi::service('angular')->getModule($formName);
+ $angModule = \Civi::service('angular')->getModule($formName);
sort($angModule['requires']);
$storedRequires = Afform::get()->addWhere('name', '=', $formName)->addSelect('requires')->execute();
$this->assertEquals(['afCore', 'mockBareFile', 'mockBespoke', 'mockFoo'], $angModule['requires']);
->setLayoutFormat('html')
->setValues(['layout' => '<div>The bare file says "<mock-bare-file/>"</div>'])
->execute();
- $angModule = Civi::service('angular')->getModule($formName);
+ $angModule = \Civi::service('angular')->getModule($formName);
sort($angModule['requires']);
$storedRequires = Afform::get()->addWhere('name', '=', $formName)->addSelect('requires')->execute();
$this->assertEquals(['afCore', 'mockBareFile', 'mockBespoke'], $angModule['requires']);
'requires' => [],
])
->execute();
- $angModule = Civi::service('angular')->getModule($formName);
+ $angModule = \Civi::service('angular')->getModule($formName);
$this->assertEquals(['afCore'], $angModule['requires']);
$storedRequires = Afform::get()->addWhere('name', '=', $formName)->addSelect('requires')->execute();
$this->assertEquals([], $storedRequires[0]['requires']);
Afform::revert()->addWhere('name', '=', $formName)->execute();
- $angModule = Civi::service('angular')->getModule($formName);
+ $angModule = \Civi::service('angular')->getModule($formName);
sort($angModule['requires']);
$this->assertEquals(['afCore', 'mockBareFile', 'mockBespoke', 'mockFoo'], $angModule['requires']);
}
<?php
+namespace api\v4\Afform;
use Civi\Test\HeadlessInterface;
use Civi\Test\TransactionalInterface;
/**
* Base class for Afform API tests.
*/
-abstract class api_v4_AfformTestCase extends \PHPUnit\Framework\TestCase implements HeadlessInterface, TransactionalInterface {
+abstract class AfformTestCase extends \PHPUnit\Framework\TestCase implements HeadlessInterface, TransactionalInterface {
/**
* Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile().
*/
public function setUp(): void {
parent::setUp();
- CRM_Core_Config::singleton()->userPermissionTemp = new CRM_Core_Permission_Temp();
- CRM_Core_Config::singleton()->userPermissionTemp->grant('administer CiviCRM');
+ \CRM_Core_Config::singleton()->userPermissionTemp = new \CRM_Core_Permission_Temp();
+ \CRM_Core_Config::singleton()->userPermissionTemp->grant('administer CiviCRM');
}
}
<?php
+namespace api\v4\Afform;
use Civi\Api4\Afform;
*
* @group headless
*/
-abstract class api_v4_AfformUsageTestCase extends api_v4_AfformTestCase {
+abstract class AfformUsageTestCase extends AfformTestCase {
use \Civi\Test\Api3TestTrait;
use \Civi\Test\ContactTestTrait;
<?php
ini_set('memory_limit', '2G');
+
// phpcs:disable
eval(cv('php:boot --level=classloader', 'phpcode'));
// phpcs:enable
-
// Allow autoloading of PHPUnit helper classes in this extension.
-foreach ([__DIR__] as $dir) {
- $loader = new \Composer\Autoload\ClassLoader();
- $loader->add('CRM_', $dir);
- $loader->add('Civi\\', $dir);
- $loader->add('api_', $dir);
- $loader->add('api\\', $dir);
- $loader->register();
-}
+$loader = new \Composer\Autoload\ClassLoader();
+$loader->add('CRM_', [__DIR__ . '/../..', __DIR__]);
+$loader->addPsr4('Civi\\', [__DIR__ . '/../../Civi', __DIR__ . '/Civi']);
+$loader->add('api_', [__DIR__ . '/../..', __DIR__]);
+$loader->addPsr4('api\\', [__DIR__ . '/../../api', __DIR__ . '/api']);
+
+$loader->register();
/**
* Call the "cv" command.
* The rest of the command to send.
* @param string $decode
* Ex: 'json' or 'phpcode'.
- * @return string
+ * @return mixed
* Response output (if the command executed normally).
+ * For 'raw' or 'phpcode', this will be a string. For 'json', it could be any JSON value.
* @throws \RuntimeException
* If the command terminates abnormally.
*/
-function cv($cmd, $decode = 'json') {
+function cv(string $cmd, string $decode = 'json') {
$cmd = 'cv ' . $cmd;
- $descriptorSpec = [0 => ["pipe", "r"], 1 => ["pipe", "w"], 2 => STDERR];
+ $descriptorSpec = [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => STDERR];
$oldOutput = getenv('CV_OUTPUT');
- putenv("CV_OUTPUT=json");
+ putenv('CV_OUTPUT=json');
// Execute `cv` in the original folder. This is a work-around for
// phpunit/codeception, which seem to manipulate PWD.
case 'phpcode':
// If the last output is /*PHPCODE*/, then we managed to complete execution.
- if (substr(trim($result), 0, 12) !== "/*BEGINPHP*/" || substr(trim($result), -10) !== "/*ENDPHP*/") {
+ if (substr(trim($result), 0, 12) !== '/*BEGINPHP*/' || substr(trim($result), -10) !== '/*ENDPHP*/') {
throw new \RuntimeException("Command failed ($cmd):\n$result");
}
return $result;