* * 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 . * * @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(); } }