set(Options::URI, 'ldap://cn=admin,dc=example,dc=org:insecure@openldap:389/dc=example,dc=org'); $options->set(Options::ENABLED, true); $options->set(Options::FILTER, 'uid=%1$s'); $options->set(Options::DEFAULT_ROLE, 'subscriber'); $options->set(Options::DEBUG, true); $options->set(Options::NAME_ATTR, 'cn'); exec(sprintf( 'wp --allow-root option update --format=json authLDAPOptions \'%1$s\'', json_encode($options->toArray()) )); } /** * @Given configuration value :arg1 is set to :arg2 */ public function configurationValueIsSetTo($arg1, $arg2) { exec(sprintf( 'wp --allow-root option patch update authLDAPOptions %1$s %2$s --format=json', $arg1, "'" . json_encode($arg2) . "'" )); } /** * @Given an LDAP user :arg1 with name :arg2, password :arg3 and email :arg4 exists */ public function anLdapUserWithNamePasswordAndEmailExists($arg1, $arg2, $arg3, $arg4) { exec(sprintf( 'ldapadd -x -H %1$s -D "%2$s" -w %3$s <res = $client->post('http://wp/wp-login.php', [ 'cookies' => CookieJar::fromArray([ 'wordpress_test_cookie' => 'test', 'XDEBUG_SESSION' => 'PHPSTORM', ], 'http://wp'), 'form_params' => [ 'log' => $arg1, 'pwd' => $arg2, ], 'allow_redirects' => false ]); } /** * @Then the login suceeds */ public function theLoginSuceeds() { Assert::isInstanceOf($this->res, Response::class); Assert::eq( $this->res->getStatusCode(), 302); Assert::startsWith($this->res->getHeader('Location')[0], 'http://localhost/wp-admin'); } /** * @Then a new WordPress user :arg1 was created with name :arg2 and email :arg3 */ public function aNewWordpressUserWasCreatedWithNameAndEmail($arg1, $arg2, $arg3) { exec(sprintf( 'wp --allow-root user get %1$s --format=json 2> /dev/null', $arg1, ), $output, $result); Assert::eq(0, $result); $user = json_decode($output[0], true); Assert::eq($user['user_email'], $arg3); Assert::eq($user['display_name'], $arg2); Assert::greaterThan( new DateTimeImmutable($user['user_registered']), (new DateTimeImmutable())->sub(new DateInterval('PT1M')), ); } /** * @Then the WordPress user :arg1 is member of role :arg2 */ public function theWordpressUserIsMemberOfRole($arg1, $arg2) { exec(sprintf( 'wp --allow-root user get %1$s --format=json 2> /dev/null', $arg1, ), $output, $result); Assert::eq(0, $result); $user = json_decode($output[0], true); $roles = array_map(function($item): string { return trim($item); }, explode(',', $user['roles'])); Assert::inArray($arg2, $roles); } /** * @Given LDAP user :arg1 is member of LDAP group :arg2 */ public function ldapUserIsMemberOfLdapGroup($arg1, $arg2) { exec(sprintf( 'ldapmodify -x -H %1$s -D "%2$s" -w %3$s 2>&1 < /dev/null', $arg1, ), $output, $result); Assert::eq(0, $result); $user = json_decode($output[0], true); $roles = array_map(function($item): string { return trim($item); }, explode(',', $user['roles'])); Assert::false(in_array($arg2, $roles)); } /** * @Given LDAP user :arg1 is not member of LDAP group :arg2 */ public function ldapUserIsNotMemberOfLdapGroup($arg1, $arg2) { exec(sprintf( 'ldapmodify -x -H %1$s -D "%2$s" -w %3$s 2>&1 <