mehrshaddarzi/wp-cli-fixtures

轻松为 WordPress 生成假数据

安装: 0

依赖关系: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 15

类型:wp-cli-package

0.3.0 2020-05-03 09:48 UTC

This package is auto-updated.

Last update: 2024-09-29 05:50:01 UTC


README

GitHub Workflow Status Packagist Downloads Packagist PHP Version Support

Faker 启发,本包提供了一个简单的方式来为您的 WordPress 安装创建大量和自定义的假数据。本包基于 nelmio/alicefzaninotto/Faker。请参考这些包的文档以了解高级用法。

wp-cli-fixtures demo

警告:本包主要用于开发目的。请自行承担使用风险,不要在生产数据库上运行,或者确保先进行备份。

快速链接: 安装 | 用法 | 贡献

安装

wp package install https://github.com/mehrshaddarzi/wp-cli-fixtures/archive/refs/heads/master.zip

需要 PHP ^7.3

用法

创建测试数据

在项目根目录下创建一个 fixtures.yml 文件(您可以在此 下载

#
# USERS
#
Hellonico\Fixtures\Entity\User:
  user{1..10}:
    user_login (unique): <username()> # '(unique)' is required
    user_pass: 123
    user_email: <freeEmail()>
    user_url: <url()>
    user_registered: <dateTimeThisDecade()>
    first_name: <firstName()>
    last_name: <lastName()>
    description: <sentence()>
    role: <randomElement(['subscriber', 'editor'])>
    meta:
      show_admin_bar_front: 'false'
      billing_phone: <phoneNumber()>
      billing_country: 'IR'
      billing_state: <randomElement(['ABZ','ADL','EAZ','WAZ','BHR','CHB','FRS','GIL','GLS','HDN','HRZ','ILM','ESF','KRN','KRH','NKH','RKH','SKH','KHZ','KBD','KRD','LRS','MKZ','MZN','GZN','QHM','SMN','SBN','THR','YZD','ZJN'])>
      billing_city: <city()>
      billing_address_1: <streetAddress()>
      billing_postcode: <postcode()>
      billing_email: '@self->user_email'
      billing_first_name: '@self->first_name'
      billing_last_name: '@self->last_name'
    acf:
      facebook_url: <url()>
      twitter_url: <url()>

#
# ATTACHMENTS
#
Hellonico\Fixtures\Entity\Attachment:
  default (template):
    post_title: <words(2, true)>
    post_date: <dateTimeThisDecade()>
    post_content: <paragraphs(5, true)>
  pics{1..3} (extends default):
    file: <picsum(<uploadDir()>, 400, 400)>
  documents{1..2} (extends default):
    file: <fileIn('C:\Users\xxx\Desktop\document', <uploadDir()>)>
  custom_images{1..4} (extends default):
    file: <fileIn('C:\Users\xxx\Desktop\wallpaper', <uploadDir()>)>

#
# TERMS
#
Hellonico\Fixtures\Entity\Term:
  category{1..10}:
    name (unique): <words(2, true)> # '(unique)' is required
    description: <sentence()>
    parent: '50%? <termId(childless=1)>' # 50% of created categories will have a top level parent category
    taxonomy: 'category' # could be skipped, default to 'category'
  tag{1..5}:
    name (unique): <words(2, true)> # '(unique)' is required
    description: <sentence()>
    taxonomy: 'post_tag'
  product_cat{1..5}: # custom taxonomy
    name (unique): <words(2, true)> # '(unique)' is required
    description: <sentences(3, true)>
    taxonomy: 'product_cat'
    acf:
      address: <streetAddress>
      zip: <postcode()>
      city: <city()>
      image: '@custom_images*->ID'

#
# POSTS
#
Hellonico\Fixtures\Entity\Post:

  # TEMPLATE
  defaultpost (template):
    post_title: <words(5, true)>
    post_date: <dateTimeThisDecade()>
    post_content: <paragraphs(5, true)>
    post_excerpt: <paragraphs(1, true)>

  # POSTS
  post{1..30} (extends defaultpost):
    meta:
      _thumbnail_id: '@custom_images*->ID'
    meta_input:
      _extra_field: <paragraphs(1, true)>
    post_category: '3x @category*->term_id' # post_category only accepts IDs
    tax_input:
      post_tag: '2x @tag*->term_id'
      # post_tag: '5x <words(2, true)> # Or tags can be dynamically created

  # PAGES
  page{contact, privacy} (extends defaultpost):
    post_title: <current()>
    post_type: page

  # CUSTOM POST TYPE
  product{1..15} (extends defaultpost):
    post_type: product
    meta:
      _thumbnail_id: '@custom_images*->ID'
    meta_input:
      _sku: <numberBetween(1000, 2000)>
      _price: <numberBetween(10000, 20000)>
      _stock_status: 'instock'
      _manage_stock: 'yes'
      _stock: <numberBetween(1, 10)>

#
# COMMENTS
#
Hellonico\Fixtures\Entity\Comment:
  comment{1..50}:
    comment_post_ID: '@post*->ID'
    user_id: '@user*->ID'
    comment_date: <dateTimeThisDecade()>
    comment_author: <username()>
    comment_author_email: <safeEmail()>
    comment_author_url: <url()>
    comment_content: <paragraphs(2, true)>
    comment_agent: <userAgent()>
    comment_author_IP: <ipv4()>
    comment_approved: 1
    comment_karma: <numberBetween(1, 100)>
    # 'meta' and 'comment_meta' are basically the same, you can use one or both,
    # they will be merged, just don't provide the same keys in each definition
    comment_meta:
      some_key: <sentence()>
    meta:
      another_key: <sentence()>

#
#  NAV MENUS
#
Hellonico\Fixtures\Entity\NavMenu:
  header:
    name: header
    locations:
      - header
      - footer

#
#  NAV MENUS ITEMS
#
Hellonico\Fixtures\Entity\NavMenuItem:
  custom_menu:
    menu_item_url: <url()>
    menu_item_title: <words(4, true)>
    menu_id: '@header->term_id'
  categories{1..3}:
    menu_item_object: '@category*'
    menu_id: '@header->term_id'
  posts{1..3}:
    menu_item_object: '@post*'
    menu_id: '@header->term_id'
  page:
    menu_item_object: '@page*'
    menu_id: '@header->term_id'

上面的示例将生成

  • 10 个用户
  • 15 个附件
  • 10 个分类
  • 40 个标签
  • 30 个带缩略图的帖子,3 个分类和 5 个标签
  • 10 个页面
  • 15 个名为 'product' 的自定义帖子类型
  • 50 条与帖子和相关用户关联的评论
  • 1 个导航菜单
  • 6 个导航菜单项

重要:请确保在它们被使用之前将引用的 ID 放置在 前面

示例:如果您在测试数据中引用了 TermAttachment 对象,则必须先放置 Post

加载测试数据

wp fixtures load

您还可以使用 --file 参数指定自定义文件

wp fixtures load --file=data.yml

删除测试数据

wp fixtures delete

您也可以删除单个测试数据类型

wp fixtures delete post

有效类型有 postattachmentcommenttermuser

向现有内容添加假数据

wp-cli-fixtures 允许您通过传递 ID 作为构造函数参数,向现有实体添加/更新内容。

向帖子 ID 1 添加/更新数据

Hellonico\Fixtures\Entity\Post:
  my_post:
    __construct: [1] # Pass your post ID as the constructor argument
    post_title: '<sentence()>'
    post_content: '<paragraphs(5, true)>'
    post_excerpt: '<paragraphs(1, true)>'

向 10 个随机现有帖子添加/更新数据

Hellonico\Fixtures\Entity\Post:
  post{1..10}:
    __construct: [<postId()>] # Use a custom formatters to return a random post ID as the constructor argument
    post_title: '<sentence()>'
    post_content: '<paragraphs(5, true)>'
    post_excerpt: '<paragraphs(1, true)>'

实体

帖子

Hellonico\Fixtures\Entity\Post 可以接受 wp_insert_post + metaacf 键中可用的任何参数。

注意:已禁用 post_date_gmtpost_modified_gmt,它们由 post_datepost_modified 设置。

附件

Hellonico\Fixtures\Entity\Attachment 可以接受 wp_insert_attachment + metafileacf 自定义键中可用的任何参数。

注意:必须通过 post_parent 键传递 parent

术语

Hellonico\Fixtures\Entity\Term 可以接受 wp_insert_term + metaacf 自定义键中可用的任何参数。

注意:必须分别通过 nametaxonomy 键传递 termtaxonomy

用户

Hellonico\Fixtures\Entity\User 可以接受 wp_insert_user + metaacf 自定义键中可用的任何参数。

评论

Hellonico\Fixtures\Entity\Comment 可以接受 wp_insert_comment + meta 自定义键中可用的任何参数。

comment_date_gmt 已禁用,它从 comment_date 设置。

导航菜单

Hellonico\Fixtures\Entity\NavMenuHellonico\Fixtures\Entity\Term 一样,是一个术语。它接受一个额外的 locations 参数来设置菜单位置。

Hellonico\Fixtures\Entity\NavMenu:
  header:
    name: header
    locations:
      - header

导航菜单项

Hellonico\Fixtures\Entity\NavMenuItem 接受与 wp_update_nav_menu_item 中的 $menu_item_data 相同的参数

注意 1:请将键中的短划线替换为下划线(例如,menu-item-object 变为 menu_item_object)。

注意 2:menu-item-object 还可以接受一个实体对象,如果是这样,则 menu-item-typemenu-item-object-id 将自动填充适当的值。

ACF 支持

每个 ACF 支持的实体(文章、术语、用户)都可以有一个 acf 键,它的工作方式与 meta 相同。

Hellonico\Fixtures\Entity\Post:
  post{1..30}:
    post_title: <words(3, true)>
    post_date: <dateTimeThisDecade()>
    acf:
      # number field
      number: <numberBetween(10, 200)>
      # repeater field
      features:
        - label: <words(2, true)>
          value: <sentence()
        - label: <words(2, true)>
          value: <sentence()>
        - label: <words(2, true)>
          value: <sentence()>

注意重复字段键,如果您有多个具有相同键的字段,则建议使用 ACF 字段键(例如 field_948d1qj5mn4d3)。

自定义格式化器

除了 fzaninotto/Faker 提供的格式化器外,您还可以使用以下自定义格式化器。

postId($args)

返回一个随机存在的文章 ID。 $args 是可选的,可以接受来自 get_posts 的任何参数

示例

<postId(category=1,2,3)>

attachmentId($args)

返回一个随机存在的附件 ID。 $args 是可选的,可以接受来自 get_posts 的任何参数

示例

<attachmentId(year=2016)>

termId($args)

返回一个随机存在的术语 ID。 $args 是可选的,可以接受来自 get_terms 的任何参数

示例

<termId(taxonomy=post_tag)>

userId($args)

返回一个随机存在的用户 ID。 $args 是可选的,可以接受来自 get_users 的任何参数

示例

<userId(role=subscriber)>

fileContent($file)

返回文件的内容。

示例

<fileContent('path/to/file.html')>

fileIn($src, $target, false)

这是对 file provider 的包装,因为一些 Faker 提供器 与 PHP 原生冲突。返回文件路径或目录中的文件名($src 相对于 fixtures.yml)。

默认目标是 WordPress 的 uploads 目录。

示例

<fileIn('my/set/of/images')>

提示

在玩 fixtures 时,database command 包可以帮助您比 wp fixtures delete 更快地重置数据库并从头开始。

贡献

此包遵循 PSR2 编码标准,并使用 Behat 进行测试。执行 composer run test 以确保您的 PR 通过。

在第一次运行之前,您需要运行 composer run prepare-tests