Extract 2 tests from AfformRoutingTest to mockPublicForm. Make example work.
authorTim Otten <totten@civicrm.org>
Wed, 10 Feb 2021 22:33:30 +0000 (14:33 -0800)
committerTim Otten <totten@civicrm.org>
Wed, 17 Feb 2021 09:24:25 +0000 (01:24 -0800)
These two cases rely on setting up the same fixture. It's easier to try out/debug if
the fixture is live/available as a matter of course.

Tangentially, if you try to use this form (whether as anonymous or as admin, it doesn't -- e.g.
because it doesn't have a "Submit" button. So this updates it.

ext/afform/mock/ang/mockPublicForm.aff.html [new file with mode: 0644]
ext/afform/mock/ang/mockPublicForm.aff.json [new file with mode: 0644]
ext/afform/mock/ang/mockPublicForm.test.php [new file with mode: 0644]
ext/afform/mock/tests/phpunit/api/v4/AfformRoutingTest.php

diff --git a/ext/afform/mock/ang/mockPublicForm.aff.html b/ext/afform/mock/ang/mockPublicForm.aff.html
new file mode 100644 (file)
index 0000000..3ee4364
--- /dev/null
@@ -0,0 +1,13 @@
+<af-form ctrl="afform">
+  <af-entity data="{contact_type: 'Individual', source: 'Hello'}" url-autofill="1" type="Contact" name="me" label="Myself" />
+  <fieldset af-fieldset="me">
+    <legend class="af-text">Individual 1</legend>
+    <div class="af-container">
+      <div class="af-container af-layout-inline">
+        <af-field name="first_name" />
+        <af-field name="last_name" />
+      </div>
+    </div>
+  </fieldset>
+  <button class="af-button btn-primary" crm-icon="fa-check" ng-click="afform.submit()">Submit</button>
+</af-form>
diff --git a/ext/afform/mock/ang/mockPublicForm.aff.json b/ext/afform/mock/ang/mockPublicForm.aff.json
new file mode 100644 (file)
index 0000000..e97ef09
--- /dev/null
@@ -0,0 +1,6 @@
+{
+  "type": "form",
+  "title": "My public form",
+  "server_route": "civicrm/mock-public-form",
+  "permission": "*always allow*"
+}
diff --git a/ext/afform/mock/ang/mockPublicForm.test.php b/ext/afform/mock/ang/mockPublicForm.test.php
new file mode 100644 (file)
index 0000000..6562912
--- /dev/null
@@ -0,0 +1,66 @@
+<?php
+
+/**
+ * @group e2e
+ * @group ang
+ */
+class MockPublicFormTest extends \Civi\AfformMock\FormTestCase {
+
+  const FILE = __FILE__;
+
+  public function testGetPage() {
+    $r = $this->createGuzzle()->get('civicrm/mock-public-form');
+    $this->assertContentType('text/html; charset=utf-8', $r);
+    $this->assertStatusCode(200, $r);
+    $body = (string) $r->getBody();
+    $this->assertContains('mockPublicForm', $body);
+  }
+
+  public function testPublicCreateAllowed() {
+    $initialMaxId = CRM_Core_DAO::singleValueQuery('SELECT max(id) FROM civicrm_contact');
+
+    $r = md5(random_bytes(16));
+
+    $me = [0 => ['fields' => []]];
+    $me[0]['fields']['first_name'] = 'Firsty' . $r;
+    $me[0]['fields']['last_name'] = 'Lasty' . $r;
+
+    $this->submit(['args' => [], 'values' => ['me' => $me]]);
+
+    // Contact was created...
+    $contact = Civi\Api4\Contact::get(FALSE)->addWhere('first_name', '=', 'Firsty' . $r)->execute()->single();
+    $this->assertEquals('Firsty' . $r, $contact['first_name']);
+    $this->assertEquals('Lasty' . $r, $contact['last_name']);
+    $this->assertTrue($contact['id'] > $initialMaxId);
+  }
+
+  public function testPublicEditDisallowed() {
+    $contact = Civi\Api4\Contact::create(FALSE)
+      ->setValues([
+        'first_name' => 'FirstBegin',
+        'last_name' => 'LastBegin',
+        'contact_type' => 'Individual',
+      ])
+      ->execute()
+      ->first();
+
+    $r = md5(random_bytes(16));
+
+    $me = [0 => ['fields' => []]];
+    $me[0]['fields']['id'] = $contact['id'];
+    $me[0]['fields']['first_name'] = 'Firsty' . $r;
+    $me[0]['fields']['last_name'] = 'Lasty' . $r;
+
+    $this->submitError(['args' => [], 'values' => ['me' => $me]]);
+    $this->assertContentType('application/json')->assertStatusCode(403);
+
+    // Contact hasn't changed
+    $get = Civi\Api4\Contact::get(FALSE)->addWhere('id', '=', $contact['id'])->execute()->single();
+    $this->assertEquals('FirstBegin', $get['first_name']);
+    $this->assertEquals('LastBegin', $get['last_name']);
+
+    // No other contacts were created or edited with the requested value.
+    $this->assertEquals(0, CRM_Core_DAO::singleValueQuery('SELECT count(*) FROM civicrm_contact WHERE first_name=%1', [1 => ["Firsty{$r}", 'String']]));
+  }
+
+}
index 3a346a0135261b9a9c1624330bc5d28fd8b18619..3fd74d34e1f81aa4edc2df96b35270e9400b6992 100644 (file)
@@ -96,92 +96,4 @@ class api_v4_AfformRoutingTest extends \PHPUnit\Framework\TestCase implements \C
     $this->assertRegExp(';afform":\{"open":"' . preg_quote($directive, ';') . '"\};', $contents);
   }
 
-  public function testPublicCreateAllowed() {
-    $initialMaxId = CRM_Core_DAO::singleValueQuery('SELECT max(id) FROM civicrm_contact');
-    $http = new \GuzzleHttp\Client(['http_errors' => FALSE]);
-    $url = function ($path, $query = NULL) {
-      return CRM_Utils_System::url($path, $query, TRUE, NULL, FALSE);
-    };
-
-    $this->createPublicForm();
-
-    $r = md5(random_bytes(16));
-
-    $me = [0 => ['fields' => []]];
-    $me[0]['fields']['first_name'] = 'Firsty' . $r;
-    $me[0]['fields']['last_name'] = 'Lasty' . $r;
-
-    $query = [
-      'params' => json_encode(['name' => $this->formName, 'args' => [], 'values' => ['me' => $me]]),
-    ];
-
-    $response = $http->post($url('civicrm/ajax/api4/Afform/submit', $query), ['headers' => ['X-Requested-With' => 'XMLHttpRequest']]);
-    $this->assertEquals(200, $response->getStatusCode());
-    $contact = Civi\Api4\Contact::get(FALSE)->addWhere('first_name', '=', 'Firsty' . $r)->execute()->first();
-    $this->assertEquals('Firsty' . $r, $contact['first_name']);
-    $this->assertEquals('Lasty' . $r, $contact['last_name']);
-    $this->assertTrue($contact['id'] > $initialMaxId);
-  }
-
-  public function testPublicEditDisallowed() {
-    $contact = Civi\Api4\Contact::create(FALSE)
-      ->setValues([
-        'first_name' => 'FirstBegin',
-        'last_name' => 'LastBegin',
-        'contact_type' => 'Individual',
-      ])
-      ->execute()
-      ->first();
-
-    $http = new \GuzzleHttp\Client(['http_errors' => FALSE]);
-    $url = function ($path, $query = NULL) {
-      return CRM_Utils_System::url($path, $query, TRUE, NULL, FALSE);
-    };
-
-    $this->createPublicForm();
-
-    $r = md5(random_bytes(16));
-
-    $me = [0 => ['fields' => []]];
-    $me[0]['fields']['id'] = $contact['id'];
-    $me[0]['fields']['first_name'] = 'Firsty' . $r;
-    $me[0]['fields']['last_name'] = 'Lasty' . $r;
-
-    $query = [
-      'params' => json_encode(['name' => $this->formName, 'args' => [], 'values' => ['me' => $me]]),
-    ];
-
-    $response = $http->post($url('civicrm/ajax/api4/Afform/submit', $query), ['headers' => ['X-Requested-With' => 'XMLHttpRequest']]);
-
-    // FIXME: The current behavior is {status=500,body='[]'} ... but status=403 probably makes more sense.
-    $this->assertEquals(500, $response->getStatusCode());
-    $get = Civi\Api4\Contact::get(FALSE)->addWhere('id', '=', $contact['id'])->execute()->first();
-    // Contact hasn't changed
-    $this->assertEquals('FirstBegin', $get['first_name']);
-    $this->assertEquals('LastBegin', $get['last_name']);
-    // No other contacts were created or edited with the requested value.
-    $this->assertEquals(0, CRM_Core_DAO::singleValueQuery('SELECT count(*) FROM civicrm_contact WHERE first_name=%1', [1 => ["Firsty{$r}", 'String']]));
-  }
-
-  private function createPublicForm():void {
-    $defaults = [
-      'title' => 'My form',
-      'name' => $this->formName,
-      'layout' => '
-<af-form ctrl="modelListCtrl">
-  <af-entity type="Contact" data="{contact_type: \'Individual\'}" name="me" label="Myself" url-autofill="1" autofill="user" />
-  <fieldset af-fieldset="me">
-      <af-field name="first_name" />
-      <af-field name="last_name" />
-  </fieldset>
-</af-form>',
-      'permission' => CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
-    ];
-    Civi\Api4\Afform::create()
-      ->setCheckPermissions(FALSE)
-      ->setLayoutFormat('html')
-      ->setValues($defaults)
-      ->execute();
-  }
-
 }