Skip to content

Instantly share code, notes, and snippets.

@swashata
Last active May 31, 2019 07:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swashata/b52e51e78bddece519601ea887d984e2 to your computer and use it in GitHub Desktop.
Save swashata/b52e51e78bddece519601ea887d984e2 to your computer and use it in GitHub Desktop.
Sample implementation of GraphQL integration test
<?php
/**
* Copyright (C) 2018 Swashata Ghosh <swashata@wpquark.com>
*
* This file is part of eForm - WordPress Builder.
*
* eForm - WordPress Builder is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* eForm - WordPress Builder is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with eForm - WordPress Builder. If not, see <http://www.gnu.org/licenses/>.
*
* @package EForm
* @subpackage Test
*/
namespace EFormTest\GraphQL\Form;
use EFormTest\EFormTestCase;
/**
* Base class for all form related tests.
*/
class Base extends EFormTestCase {
protected function get_empty_create_form_mutation() : string {
return <<<'GQL'
mutation EmptyCreateForm($name: String!) {
createForm(data: {
name: $name
structure: []
pool: []
conditionals: []
categories: []
editors:[]
settings: {}
elements: []
}) {
id
name
owner {
id
}
}
}
GQL;
}
protected function exec_empty_create_form_mutation( array $variable = [ 'name' => 'Hello Form' ] ) : array {
return self::executeGraphQLQuery(
$this->get_empty_create_form_mutation(),
$variable
);
}
protected function get_create_form_mutation_with_settings() : string {
return <<<'GQL'
mutation CreateFormWithSettings($settings: FormSettingsInput!) {
createForm(data: {
name:"Form with custom settings"
structure: []
pool: [
{
id: "123443",
name: "Quizzes",
elements: []
}
]
conditionals: []
categories: []
editors:[]
settings: $settings,
elements: []
}) {
id
name
settings {
behavior {
autoProgress
autoProgressDelay
autoSubmit
autoScroll
noticeIfSubmitted
submittedNotice
}
restrictions {
blockPreviousNavigation
paginationRestriction
}
accessPermission {
canEdit
editTime
restrictEditOnExpiration
}
submissionPermission {
limitTotal
totalLimitMsg
totalLimitNotice
limitPerEmail
emailLimitMsg
limitPerIp
ipLimitMsg
limitPerUser
userLimitMsg
limitPerCookie
cookieLimitMsg
limitOnLogin
loginLimitMsg
showLoginFormOnLoggedOut
limitOnInterval
intervalLimitMsg
startsOn
startsOnMsg
expiresOn
expiresOnMsg
}
userNotification {
subject
message
updateSubject
updateMessage
fromName
fromEmail
header
showSubmission
viewOnline
viewOnlineText
footer
}
adminNotification {
toEmail
subject
message
updateSubject
updateMessage
fromName
fromEmail
header
replyToUser
showSubmission
footer
}
appearance {
controlTypes
maxWidth
layout
showTabs
showProgressBar
progressDecimalPoint
progressBarPosition
}
theme {
scheme
customColorPrimary
customColorSecondary
customColorBg
css
}
customBackground {
enabled
backgroundImage
backgroundPosition
backgroundSize
backgroundRepeat
backgroundOrigin
backgroundClip
backgroundAttachment
}
typography {
baseFont
bodyFamily
headFamily
headFontBold
headFontItalic
}
}
}
}
GQL;
}
protected function exec_create_form_mutation_with_settings( array $settings = [] ) : array {
return self::executeGraphQLQuery(
$this->get_create_form_mutation_with_settings(),
[
'settings' => $settings,
]
);
}
protected function get_create_form_mutation_with_elements() : string {
return <<<'GQL'
mutation CreateFormWithElements($structure: [FormPageInput!]!, $elements: [FormElementInput!]!) {
createForm(data: {
name:"Form with custom settings"
structure: $structure
pool: []
conditionals: []
categories: []
editors:[]
settings: {},
elements: $elements
}) {
id
structure {
id
children
}
elements {
id
type
}
}
}
GQL;
}
protected function exec_create_form_mutation_with_elements( array $structure, array $elements ) : array {
return self::executeGraphQLQuery(
$this->get_create_form_mutation_with_elements(),
[
'structure' => $structure,
'elements' => $elements,
]
);
}
}
<?php
/**
* Copyright (C) 2018 Swashata Ghosh <swashata@wpquark.com>
*
* This file is part of eForm - WordPress Builder.
*
* eForm - WordPress Builder is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* eForm - WordPress Builder is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with eForm - WordPress Builder. If not, see <http://www.gnu.org/licenses/>.
*
* @package EForm
* @subpackage Test
*/
namespace EFormTest\GraphQL\Form;
use EForm\Factory\Form\Settings;
/**
* @testdox 💾 Mutation `createForm`
* @group createForm
* @group GraphQL
*/
class CreateFormTest extends Base {
// Test access
/**
* @testdox blocks non-authenticated users
*
* @return void
*/
public function testBlocksNonAuthenticatedUsers() {
$this->loginAsAndAssert(
[ 'author', 'contributor', 'subscriber', false ],
function() {
$result = $this->exec_empty_create_form_mutation();
$this->assertArrayHasKey( 'errors', $result );
$this->assertEquals( 'Current user can not create the resource', $result['errors'][0]['message'] );
}
);
}
/**
* @testdox works for authenticated users
*
* @return void
*/
public function testWorksForAuthenticatedUsers() {
$this->loginAsAndAssert(
[ 'administrator', 'editor' ],
function() {
$result = $this->exec_empty_create_form_mutation();
$this->assertArrayNotHasKey( 'errors', $result );
$this->assertEquals(
$result['data']['createForm']['owner']['id'],
(string) \get_current_user_id()
);
}
);
}
/**
* @testdox name works
*
* @return void
*/
public function testNameWorks() {
$this->loginAsAndAssert(
[ 'administrator', 'editor' ],
function() {
$result = $this->exec_empty_create_form_mutation(
[
'name' => 'My Form',
]
);
$this->assertArrayNotHasKey( 'errors', $result );
$this->assertEquals( 'My Form', $result['data']['createForm']['name'] );
}
);
}
/**
* @testdox default settings works
*
* @return void
*/
public function testDefaultSettingsWorks() {
$default_settings = Settings::get_all_settings_default_value();
$this->loginAsAndAssert(
[ 'administrator', 'editor' ],
function() use ( $default_settings ) {
$result = $this->exec_create_form_mutation_with_settings();
$this->assertArrayNotHasKey( 'errors', $result );
$settings = $result['data']['createForm']['settings'];
foreach ( $default_settings as $s_key => $s_value ) {
// If you are seeing error here, then update get_create_form_mutation_with_settings
// method with updated settings structure.
$this->assertArrayHasKey( $s_key, $settings );
// don't assert equality directly, as it will fail for enums and
// lists, rather make sure the key exists and that it is not null
// if the default value isn't null
foreach ( $s_value as $key => $val ) {
$this->assertArrayHasKey( $key, $settings[ $s_key ] );
if ( ! is_null( $val ) ) {
$this->assertNotNull( $settings[ $s_key ][ $key ] );
} else {
$this->assertNull( $settings[ $s_key ][ $key ] );
}
}
}
}
);
}
/**
* @testdox custom value on settings works
*
* @return void
*/
public function testCustomValueOnSettingsWorks() {
$custom_settings = [
'behavior' => [
'autoProgress' => true,
'autoProgressDelay' => 900,
'autoSubmit' => false,
'autoScroll' => true,
'noticeIfSubmitted' => true,
'submittedNotice' => 'Some custom message from end user',
],
'restrictions' => [
'blockPreviousNavigation' => false,
'paginationRestriction' => 'NONE',
],
'accessPermission' => [
'canEdit' => true,
'editTime' => 876,
'restrictEditOnExpiration' => false,
],
'submissionPermission' => [
'limitTotal' => 100,
'totalLimitMsg' => 'Something awesome about total limit',
'totalLimitNotice' => 'Something noticy about total limit',
'limitPerEmail' => true,
'emailLimitMsg' => 'Some message about email limit',
'limitPerIp' => true,
'ipLimitMsg' => 'Some message about ip limit',
'limitPerUser' => true,
'userLimitMsg' => 'Some message about user limit',
'limitPerCookie' => true,
'cookieLimitMsg' => 'Some message about cookie limit',
'limitOnLogin' => 'LOGGED_IN',
'loginLimitMsg' => 'Some message about login limit',
'showLoginFormOnLoggedOut' => true,
'limitOnInterval' => true,
'intervalLimitMsg' => 'Some message about interval limit',
'startsOn' => true,
'startsOnMsg' => 'Some message about starts on limit',
'expiresOn' => true,
'expiresOnMsg' => 'Some message about expires on limit',
],
'userNotification' => [
'subject' => 'Some text',
'message' => 'Some text',
'updateSubject' => 'Some text',
'updateMessage' => 'Some text',
'fromName' => 'Some text',
'fromEmail' => 'Some text',
'header' => 'Some text',
'showSubmission' => true,
'viewOnline' => true,
'viewOnlineText' => 'Some text',
'footer' => 'Some text',
],
'adminNotification' => [
'toEmail' => 'Some text for admin notification',
'subject' => 'Some text for admin notification',
'message' => 'Some text for admin notification',
'updateSubject' => 'Some text for admin notification',
'updateMessage' => 'Some text for admin notification',
'fromName' => 'Some text for admin notification',
'fromEmail' => 'Some text for admin notification',
'header' => 'Some text for admin notification',
'replyToUser' => true,
'showSubmission' => true,
'footer' => 'Some text for admin notification',
],
'appearance' => [
'controlTypes' => 'MATERIAL',
'maxWidth' => '960px',
'layout' => 'FIXED',
'showTabs' => false,
'showProgressBar' => true,
'progressDecimalPoint' => 4,
'progressBarPosition' => 'BOTTOM',
],
'theme' => [
'scheme' => 'blue-gray',
'customColorPrimary' => '#ff0000',
'customColorSecondary' => '#ffff00',
'customColorBg' => '#0000ff',
'css' => '.foo { display: grid; }',
],
'customBackground' => [
'enabled' => true,
'backgroundImage' => 'https://example.com/image.png',
'backgroundPosition' => 'foo',
'backgroundSize' => 'bar',
'backgroundRepeat' => 'some prop',
'backgroundOrigin' => 'some prop',
'backgroundClip' => 'some prop',
'backgroundAttachment' => 'some prop',
],
'typography' => [
'baseFont' => 'Some font',
'bodyFamily' => 'Some font',
'headFamily' => 'Some font',
'headFontBold' => true,
'headFontItalic' => true,
],
];
$this->loginAsAndAssert(
[ 'administrator', 'editor' ],
function() use ( $custom_settings ) {
$result = $this->exec_create_form_mutation_with_settings(
$custom_settings
);
$this->assertArrayNotHasKey( 'errors', $result );
foreach ( $custom_settings as $key => $value ) {
$this->assertEquals( $value, $result['data']['createForm']['settings'][ $key ] );
}
}
);
}
/**
* @testdox throws error if two elements have same id
*
* @group formModelValidation
*
* @return void
*/
public function testThrowsErrorIfTwoElementsHaveSameId() {
$this->loginAsAndAssert(
[ 'administrator', 'editor' ],
function() {
$result = $this->exec_create_form_mutation_with_elements(
// structure
[
[
'id' => 's-123',
'config' => [
'title' => 'Foo',
'subtitle' => 'Foo',
'icon' => 'fa',
'timer' => 123,
],
'children' => [],
],
],
// element
[
[
'id' => '123',
'type' => 'checkbox',
'config' => [],
'children' => [],
],
[
'id' => '123',
'type' => 'radio',
'config' => [],
'children' => [],
],
]
);
$this->assertArrayHasKey( 'errors', $result );
$this->assertEquals(
'Ids for all elements items must be unique.',
$result['errors'][0]['message']
);
}
);
}
/**
* @testdox one element can not appear in more than one structure/element/pool
*
* @group formModelValidation
*
* @return void
*/
public function testOneElementCanNotAppearInMoreThanOneStructureElementOrPool() {
$this->loginAsAndAssert(
[ 'administrator', 'editor' ],
function() {
$result = $this->exec_create_form_mutation_with_elements(
// structure
[
[
'id' => 's-123',
'config' => [
'title' => 'Foo',
'subtitle' => 'Foo',
'icon' => 'fa',
'timer' => 123,
],
'children' => [ '123', '1234' ],
],
],
// element
[
[
'id' => '123',
'type' => 'checkbox',
'config' => [],
'children' => [],
],
[
'id' => '1234',
'type' => 'radio',
'config' => [],
'children' => [ '123' ],
],
]
);
$this->assertArrayHasKey( 'errors', $result );
$this->assertEquals(
'An element can only appear once in a structure, pool or another element. Element::123 is positioned in structure with id::s-123 and element with id::1234.',
$result['errors'][0]['message']
);
}
);
}
/**
* @testdox throws error if element is child of itself
*
* @group formModelValidation
*
* @return void
*/
public function testThrowsErrorIfElementIsChildOfItself() {
$this->loginAsAndAssert(
[ 'administrator', 'editor' ],
function() {
$result = $this->exec_create_form_mutation_with_elements(
// structure
[
[
'id' => 's-123',
'config' => [
'title' => 'Foo',
'subtitle' => 'Foo',
'icon' => 'fa',
'timer' => 123,
],
'children' => [ '1234' ],
],
],
// element
[
[
'id' => '123',
'type' => 'checkbox',
'config' => [],
'children' => [ '123' ],
],
[
'id' => '1234',
'type' => 'radio',
'config' => [],
'children' => [],
],
]
);
$this->assertArrayHasKey( 'errors', $result );
$this->assertEquals(
'An element can not be a child of itself. Check Element::123.',
$result['errors'][0]['message']
);
}
);
}
/**
* @testdox throws error if element is orphaned
*
* @group formModelValidation
*
* @return void
*/
public function testThrowsErrorIfElementIsOrphaned() {
$this->loginAsAndAssert(
[ 'administrator', 'editor' ],
function() {
$result = $this->exec_create_form_mutation_with_elements(
// structure
[
[
'id' => 's-123',
'config' => [
'title' => 'Foo',
'subtitle' => 'Foo',
'icon' => 'fa',
'timer' => 123,
],
'children' => [ '123' ],
],
],
// element
[
[
'id' => '123',
'type' => 'checkbox',
'config' => [],
'children' => [],
],
// error will be thrown here
[
'id' => '1234',
'type' => 'radio',
'config' => [],
'children' => [],
],
[
'id' => '12345',
'type' => 'radio',
'config' => [],
'children' => [],
],
]
);
$this->assertArrayHasKey( 'errors', $result );
$this->assertEquals(
'Element::1234 is not positioned anywhere and can not be left orphaned.',
$result['errors'][0]['message']
);
}
);
}
/**
* @testdox throws error if elements have simple circular dependency
*
* @group formModelValidation
*
* @return void
*/
public function testThrowsErrorIfElementsHaveSimpleCircularDependency() {
$this->loginAsAndAssert(
[ 'administrator', 'editor' ],
function() {
$result = $this->exec_create_form_mutation_with_elements(
// structure
[
[
'id' => 's-123',
'config' => [
'title' => 'Foo',
'subtitle' => 'Foo',
'icon' => 'fa',
'timer' => 123,
],
'children' => [],
],
],
// element
[
[
'id' => '123',
'type' => 'checkbox',
'config' => [],
'children' => [ '1234' ],
],
[
'id' => '1234',
'type' => 'radio',
'config' => [],
'children' => [ '123' ],
],
]
);
$this->assertArrayHasKey( 'errors', $result );
$this->assertEquals(
'Elements have circular dependency. Check the following elements: 1234->123->1234.',
$result['errors'][0]['message']
);
}
);
}
/**
* @testdox throws error if elements have complex circular dependency
*
* @group formModelValidation
*
* @return void
*/
public function testThrowsErrorIfElementsHaveComplexCircularDependency() {
$this->loginAsAndAssert(
[ 'administrator', 'editor' ],
function() {
$result = $this->exec_create_form_mutation_with_elements(
// structure
[
[
'id' => 's-123',
'config' => [
'title' => 'Foo',
'subtitle' => 'Foo',
'icon' => 'fa',
'timer' => 123,
],
'children' => [],
],
],
// element
[
[
'id' => '123',
'type' => 'checkbox',
'config' => [],
'children' => [ '1234', 'aaaa' ],
],
[
'id' => '1234',
'type' => 'radio',
'config' => [],
'children' => [ '12345' ],
],
[
'id' => '12345',
'type' => 'radio',
'config' => [],
'children' => [ '123456', 'bbbb', 'cccc' ],
],
[
'id' => '123456',
'type' => 'radio',
'config' => [],
'children' => [ '1234567', 'eeee', 'ffff' ],
],
[
'id' => '1234567',
'type' => 'radio',
'config' => [],
'children' => [ '123', 'dddd' ],
],
[
'id' => 'aaaa',
'type' => 'radio',
'config' => [],
'children' => [],
],
[
'id' => 'bbbb',
'type' => 'radio',
'config' => [],
'children' => [],
],
[
'id' => 'cccc',
'type' => 'radio',
'config' => [],
'children' => [],
],
[
'id' => 'dddd',
'type' => 'radio',
'config' => [],
'children' => [],
],
[
'id' => 'eeee',
'type' => 'radio',
'config' => [],
'children' => [],
],
[
'id' => 'ffff',
'type' => 'radio',
'config' => [],
'children' => [],
],
]
);
$this->assertArrayHasKey( 'errors', $result );
$this->assertEquals(
'Elements have circular dependency. Check the following elements: 1234->12345->123456->1234567->123->1234.',
$result['errors'][0]['message']
);
}
);
}
/**
* @testdox works good if none of the exceptions are met
*
* @group formModelValidation
*
* @return void
*/
public function testWorksGoodIfNoneOfTheExceptionsAreMet() {
$elements = [
[
'id' => '123',
'type' => 'checkbox',
'config' => [],
'children' => [ 'aaaa', 'dddd' ],
],
[
'id' => '1234',
'type' => 'radio',
'config' => [],
'children' => [ 'bbbb' ],
],
[
'id' => '12345',
'type' => 'radio',
'config' => [],
'children' => [ 'cccc' ],
],
[
'id' => '123456',
'type' => 'radio',
'config' => [],
'children' => [ 'eeee', 'ffff' ],
],
[
'id' => 'aaaa',
'type' => 'radio',
'config' => [],
'children' => [],
],
[
'id' => 'bbbb',
'type' => 'radio',
'config' => [],
'children' => [],
],
[
'id' => 'cccc',
'type' => 'radio',
'config' => [],
'children' => [],
],
[
'id' => 'dddd',
'type' => 'radio',
'config' => [],
'children' => [],
],
[
'id' => 'eeee',
'type' => 'radio',
'config' => [],
'children' => [],
],
[
'id' => 'ffff',
'type' => 'radio',
'config' => [],
'children' => [],
],
];
$structure = [
[
'id' => 's-123',
'config' => [
'title' => 'Foo',
'subtitle' => 'Foo',
'icon' => 'fa',
'timer' => 123,
],
'children' => [ '123', '1234' ],
],
[
'id' => 's-1234',
'config' => [
'title' => 'Foo',
'subtitle' => 'Foo',
'icon' => 'fa',
'timer' => 123,
],
'children' => [ '123456', '12345' ],
],
];
$this->loginAsAndAssert(
[ 'administrator', 'editor' ],
function() use ( $structure, $elements ) {
$result = $this->exec_create_form_mutation_with_elements(
// structure
$structure,
// element
$elements
);
$this->assertArrayNotHasKey( 'errors', $result );
foreach ( $elements as $elm_key => $elm ) {
$this->assertEquals(
$elm['id'],
$result['data']['createForm']['elements'][ $elm_key ]['id']
);
$this->assertEquals(
$elm['type'],
$result['data']['createForm']['elements'][ $elm_key ]['type']
);
}
foreach ( $structure as $str_key => $str ) {
$this->assertEquals(
$str['id'],
$result['data']['createForm']['structure'][ $str_key ]['id']
);
$this->assertEquals(
$str['children'],
$result['data']['createForm']['structure'][ $str_key ]['children']
);
}
}
);
}
}
<?php
/**
* Copyright (C) 2018 Swashata Ghosh <swashata@wpquark.com>
*
* This file is part of eForm - WordPress Builder.
*
* eForm - WordPress Builder is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* eForm - WordPress Builder is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with eForm - WordPress Builder. If not, see <http://www.gnu.org/licenses/>.
*
* @package EFormTest
* @subpackage Test
*/
namespace EFormTest;
use GraphQL\GraphQL;
use EForm\GraphQL\Schema;
use GraphQL\Error\Debug;
// @codeCoverageIgnoreStart
if ( ! defined( 'ABSPATH' ) ) {
die( '' );
}
// @codeCoverageIgnoreEnd
/**
* An abstract class that inherits from WP_UnitTestCase and provides some helpers
* for easy Integration and GraphQL tests for our system.
*/
abstract class EFormTestCase extends \WP_UnitTestCase {
const BUILT_IN_ROLES = [
'administrator',
'editor',
'author',
'contributor',
'subscriber',
];
/**
* Execute a GraphQL Query on our Schema.
*
* @param string $query GraphQL Query.
* @param mixed[] $variable_values Variables passed to the query.
* @return array
*/
protected static function executeGraphQLQuery( $query, $variable_values = null ) : array {
$debug = Debug::INCLUDE_DEBUG_MESSAGE | Debug::INCLUDE_TRACE;
return GraphQL::executeQuery(
Schema::get_schema(),
$query,
null,
null,
$variable_values
)->toArray( $debug );
}
/**
* Check if the query yields expected result.
*
* @param string $query The query string.
* @param array $expected Associative array of expected result.
* @param mixed[] $variable_values Variables passed to the query.
* @return void
*/
protected static function assertValidGraphQLQuery( $query, $expected, $variable_values = null ) : void {
$result = self::executeGraphQLQuery( $query, $variable_values )['data'];
self::assertEquals(
$expected,
$result
);
}
/**
* Logout current user.
*
* @return void
*/
protected function logout() {
global $current_user;
\wp_logout();
$current_user = null;
}
/**
* Login to WordPress system as an administrator.
*
* @return \WP_User
*/
protected function loginAsAdministrator() {
return $this->loginAsRole( 'administrator' );
}
/**
* Login to WordPress system as an editor.
*
* @return \WP_User
*/
protected function loginAsEditor() {
return $this->loginAsRole( 'editor' );
}
/**
* Login to WordPress system as an author.
*
* @return \WP_User
*/
protected function loginAsAuthor() {
return $this->loginAsRole( 'author' );
}
/**
* Login to WordPress system as an contributor.
*
* @return \WP_User
*/
protected function loginAsContributor() {
return $this->loginAsRole( 'contributor' );
}
/**
* Login to WordPress system as an subscriber.
*
* @return \WP_User
*/
protected function loginAsSubscriber() {
return $this->loginAsRole( 'subscriber' );
}
/**
* Login to WordPress system as an the given role.
*
* @return \WP_User
*/
protected function loginAsRole( $role ) {
if ( ! in_array( $role, self::BUILT_IN_ROLES ) ) {
throw new \Exception( 'Can only login as one of the build-in roles, not ' . $role );
}
$this->logout();
$uid = \wp_generate_uuid4();
$user_login = 'eform_' . $uid . $role;
$user_password = \wp_generate_password();
$user = $this->factory()->user->create_and_get(
[
'user_login' => $user_login,
'user_pass' => $user_password,
'user_email' => $uid . '@wpeform.io',
'role' => $role,
]
);
\wp_signon(
[
'user_login' => $user_login,
'user_password' => $user_password,
'rememberme' => false,
]
);
\wp_set_current_user( $user->ID );
return $user;
}
/**
* Login as a user with specified role and assert the test.
*
* All assertions are actually done by calling the callback. If you pass in
* `false` inside `$roles` array, then the function is called against logged
* out state.
*
* @param array $roles Array of roles.
* @param callable $function Assertion function to call.
* @return void
*/
protected function loginAsAndAssert( array $roles, callable $function ) {
foreach ( $roles as $role ) {
if ( false === $role ) {
$this->logout();
} else {
$this->loginAsRole( $role );
}
$function();
}
$this->logout();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment