Skip to content

Commit

Permalink
Merge pull request #25 from 5queezer/master
Browse files Browse the repository at this point in the history
Fake user data capability extension
  • Loading branch information
haroldangenent authored Feb 8, 2019
2 parents aa36a12 + da7993f commit aee5ca2
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 12 deletions.
36 changes: 33 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Fake post data with [wp-cli](https://github.com/wp-cli/wp-cli). Made for WordPre

Relies on [nelmio/alice](https://github.com/nelmio/alice) and [fzaninotto/Faker](https://github.com/fzaninotto/Faker).

Extended with user capabilities by [5queezer/faker](https://github.com/5queezer/faker).

## Install
```
wp package install trendwerk/faker
Expand Down Expand Up @@ -35,6 +37,7 @@ The YAML file supports:
| Fields | Description |
| :--- | :--- |
| `WP_Post` | All properties from [`wp_insert_post`](https://developer.wordpress.org/reference/functions/wp_insert_post/)
| `WP_User` | All properties from [`wp_insert_user`](https://developer.wordpress.org/reference/functions/wp_insert_user/)
| `meta` | Post meta
| `terms` | Terms for taxonomies, see [Terms](https://github.com/trendwerk/faker#terms)
| `acf` | [Advanced Custom Fields](https://www.advancedcustomfields.com/) fields, see [ACF](https://github.com/trendwerk/faker#acf)
Expand Down Expand Up @@ -128,11 +131,27 @@ Trendwerk\Faker\Entity\Post:
address: '<address()>'
```

### Users

```yaml
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: 'author'
```

Generates 10 users with the role of an author.

### Attachments

Currently the only type of supported attachments are images.


#### Images
```yaml
Trendwerk\Faker\Entity\Image:
Expand All @@ -142,28 +161,39 @@ Trendwerk\Faker\Entity\Image:

Generates 3 image attachments. Images are provided by [Faker](https://github.com/fzaninotto/Faker#fakerproviderimage), which in turn are provided by [LoremPixel](http://lorempixel.com/).

#### Post + (Featured) Image
#### Post + (Featured) Image + User
```yaml
# image.yml
Trendwerk\Faker\Entity\Image:
image{1..3}:
data: '<image()>'
```

```yaml
# user.yml
Trendwerk\Faker\Entity\User:
user{1..10}:
user_login: '<username()>'
user_pass: '<username()>'
user_email: '<email()>'
role: 'author'
```

```yaml
# post.yml
Trendwerk\Faker\Post:
post{1..1}:
post_content: <paragraphs(3, true)>
post_title: '<sentence()>'
post_author: '@user*->id'
meta:
_thumbnail_id: '@image*->id'
```

You can now supply both files to `wp faker fake`:
You can now supply all three files to `wp faker fake`:

```sh
wp faker fake image.yml post.yml
wp faker fake image.yml user.yml post.yml
```

**Make sure you load the file that contains the referenced objects first.**
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "trendwerk/faker",
"description": "Fake post data with WP-CLI.",
"description": "Fake data with WP-CLI.",
"type": "wp-cli-package",
"homepage": "https://github.com/trendwerk/faker",
"license": "GPL-3.0+",
Expand Down
4 changes: 2 additions & 2 deletions features/fake-attachment.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Feature: Fake attachments
When I run `wp faker fake image.yml`
Then STDOUT should contain:
"""
Generated 3 new posts.
Generated 3 new objects.
"""

When I run `wp post list --post_type=attachment --post_mime_type=image/jpeg --meta_key=_fake --format=count`
Expand Down Expand Up @@ -43,7 +43,7 @@ Feature: Fake attachments
When I run `wp faker fake image.yml post.yml`
Then STDOUT should contain:
"""
Generated 2 new posts.
Generated 2 new objects.
"""

When I run `wp post list --post_type=attachment --post_mime_type=image/jpeg --meta_key=_fake --format=ids`
Expand Down
8 changes: 4 additions & 4 deletions features/fake-post.feature
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Feature: Fake posts
When I run `wp faker fake post.yml`
Then STDOUT should contain:
"""
Generated 10 new posts.
Generated 10 new objects.
"""

When I run `wp post list --meta_key=_fake --format=count`
Expand All @@ -35,7 +35,7 @@ Feature: Fake posts
When I run `wp faker fake post.yml`
Then STDOUT should contain:
"""
Generated 10 new posts.
Generated 10 new objects.
"""

When I run `wp post list --meta_key=_fake --format=count`
Expand All @@ -58,7 +58,7 @@ Feature: Fake posts
When I run `wp faker fake post.yml`
Then STDOUT should contain:
"""
Generated 30 new posts.
Generated 30 new objects.
"""

When I run `wp post list --meta_key=_fake --format=count --post_type=page`
Expand All @@ -82,7 +82,7 @@ Feature: Fake posts
When I run `wp faker fake post.yml`
Then STDOUT should contain:
"""
Generated 15 new posts.
Generated 15 new objects.
"""

When I run `wp post list --meta_key=_fake --format=count`
Expand Down
28 changes: 28 additions & 0 deletions features/fake-user.feature
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
"""
2 changes: 1 addition & 1 deletion init.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
}

WP_CLI::add_command('faker', __NAMESPACE__ . '\\Command', [
'shortdesc' => 'Fake post data with WP-CLI.',
'shortdesc' => 'Fake data with WP-CLI.',
]);
4 changes: 3 additions & 1 deletion src/Command.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Trendwerk\Faker;

use WP_CLI;
Expand Down Expand Up @@ -28,7 +29,7 @@ public function fake(array $files = [])

$progressBar->finish();

WP_CLI::success(sprintf('Generated %d new posts.', $objectCount));
WP_CLI::success(sprintf('Generated %d new objects.', $objectCount));
}

/**
Expand All @@ -47,6 +48,7 @@ public function delete(array $args, array $assocArgs)
$dataTypes = [
'Attachment',
'Post',
'User',
];

foreach ($dataTypes as $dataType) {
Expand Down
73 changes: 73 additions & 0 deletions src/Entity/User.php
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);
}
}

0 comments on commit aee5ca2

Please sign in to comment.