-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from 5queezer/master
Fake user data capability extension
- Loading branch information
Showing
8 changed files
with
145 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
Feature: Fake users | ||
|
||
Scenario: Generate user | ||
Given a WP install | ||
And a user.yml file: | ||
""" | ||
Trendwerk\Faker\Entity\User: | ||
user{1..10}: | ||
user_login: '<username()>' | ||
user_pass: '<username()>' | ||
first_name: '<firstName()>' | ||
last_name: '<lastName()>' | ||
display_name: '<firstName()> <lastName()>' | ||
user_email: '<email()>' | ||
role: 'editor' | ||
""" | ||
|
||
When I run `wp faker fake user.yml` | ||
Then STDOUT should contain: | ||
""" | ||
Generated 10 new objects. | ||
""" | ||
|
||
When I run `wp user list --role=editor --meta_key=_fake --format=count` | ||
Then STDOUT should be: | ||
""" | ||
10 | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace Trendwerk\Faker\Entity; | ||
|
||
use WP_User_Query; | ||
|
||
class User extends Entity | ||
{ | ||
public $user_pass; | ||
public $user_login; | ||
public $user_nicename; | ||
public $user_url; | ||
public $user_email; | ||
public $display_name; | ||
public $nickname; | ||
public $first_name; | ||
public $last_name; | ||
public $description; | ||
public $rich_editing; | ||
public $syntax_highlighting; | ||
public $comment_shortcuts; | ||
public $admin_color; | ||
public $use_ssl; | ||
public $user_registered; | ||
public $show_admin_bar_front; | ||
public $role; | ||
public $locale; | ||
public $meta; | ||
|
||
public function persist() | ||
{ | ||
$this->id = $this->create(); | ||
|
||
update_user_meta($this->id, '_fake', true); | ||
|
||
if ($this->meta) { | ||
foreach ($this->meta as $key => $value) { | ||
update_user_meta($this->id, $key, $value); | ||
} | ||
} | ||
} | ||
|
||
public static function delete() | ||
{ | ||
$query = new WP_User_Query([ | ||
'fields' => 'ids', | ||
'meta_query' => [ | ||
[ | ||
'key' => '_fake', | ||
'value' => true, | ||
], | ||
], | ||
]); | ||
|
||
foreach ($query->results as $id) { | ||
wp_delete_user($id); | ||
} | ||
|
||
return count($query->results); | ||
} | ||
|
||
protected function create() | ||
{ | ||
return wp_insert_user($this->getUserData()); | ||
} | ||
|
||
protected function getUserData() | ||
{ | ||
$data = get_object_vars($this); | ||
|
||
return array_filter($data); | ||
} | ||
} |