Data generators for local plugins

Data generators for local plugins

Luke Carrier -
回帖数:4
Hi all,

I've been working on adding acceptance tests to the CPD plugin (mostly as a learning exercise for myself).

One vital part of these tests is creating test data ("fixtures"). It seems as though Moodle's provision for this is in the form of data generators, which Behat should instantiate and call methods on as appropriate. It doesn't appear as though data generators have been implemented outside of blocks and activity modules, though?

/local/cpd/tests/behat/manage_own_cpd.feature
  @javascript
  Scenario: Delete own CPD record
    Given the following "users" exist:
      | username | firstname | lastname | email         |
      | user1    | User      | 1        | user1@asd.com |
    And the following "cpd_activity_types" exist:
      | Activity Type |
      | Behatting     |
    And the following "cpd activities" exist:
      | Development Need | Objective | Activity Type | Activity Description |
      | Behat            | Behat     | Behatting     | Behatting            |
  

/local/cpd/tests/generator/lib.php
<?php

class local_cpd_generator extends component_generator_base {
    public function create_cpd_activity_type() {
  // forgive me
xdebug_print_function_stack("You'll have a job!"); var_dump(func_get_args()); die(); } }
If anyone familiar with the testing infrastructure in Moodle could comment on this I'd really appreciate it.

Regards,
Luke.
回复Luke Carrier

Re: Data generators for local plugins

Luke Carrier -
The offending code appears to be in the behat_data_generators class declared in /lib/tests/behat/behat_data_generators.php. For some reason, the generators acceptable for creating fixtures in Behat have been hardcoded.

Why? Would it not make more sense to have a generator factory and allow each *_generator to provide an array of its available generators? In its current state, Behat is pretty much useless to plugin maintainers; I'd be happy to contribute a patch if it's likely to be merged.
回复Luke Carrier

Re: Data generators for local plugins

Eloy Lafuente (stronk7) -
Core developers的头像 Documentation writers的头像 Moodle HQ的头像 Particularly helpful Moodlers的头像 Peer reviewers的头像 Plugin developers的头像 Testers的头像

Hi Luke,

yes, unless I'm wrong, while phpunit is perfectly able to use any generator from any plugin via testing_data_generator->get_plugin_generator($component), behat support for them is still restricted to those defined/hardcoded @ behat_data_generators::$elements.

Basically what is being missed is the "metadata" needed to convert the human-like nouns ("users", "course enrolments"...) into their php-level counterparts ("user", "enrol_user") and a few more dependencies.

With that and a mechanism to know to which plugin to point to it should be really easy to allow plugins to declare such metadata and behat to support the use of those plugin generators easily.

I've been searching in the Tracker, coz it sounded to me a lot that we already had an issue to add support for plugin generators in behat, but have failed so far.

So, I'd recommend you to create an issue about it, ideally with some base implementation to discuss about, it would be really welcome and would put finally generators support from behat on par with phpunit support.

Not sure if we should extend the current the_following_exist() to support some "component/element" syntax in the current parameter or instead a create new "The following component plugin element exist" step, completely separated from the existing one. Perhaps the later is better, and will work better in the long term, avoiding duplicated/conflicting terms and so on...

In any case, better to be discussed in a Tracker issue, indeed.

TIA and ciao 微笑

回复Eloy Lafuente (stronk7)

Re: Data generators for local plugins

Luke Carrier -

Hi Eloy,

Thanks for the clarification Eloy, it's good to know I'm not going crazy 眨眼

I'm inclined to agree with the latter syntax to avoid conflicts down the line. Tracker ticket MDL-48024 opened!

Regards,
Luke.

回复Luke Carrier

Re: Data generators for local plugins

Marina Glancy -
Core developers的头像 Moodle HQ的头像 Moodle Workplace team的头像 Peer reviewers的头像 Plugin developers的头像 Testers的头像

Hello Luke,

I suggest you to define your own behat step for your local plugin.

If you need a generator for behat only and you don't need phpunit at all, you don't even need to create generator - just a behat step definition.

See https://docs.moodle.org/dev/Acceptance_testing#Adding_steps_definitions about how to create step definitions.

It is an interesting suggestion to allow this moodle step to work with any generator but we will miss on all pre- and post- processing (the phpunit generator will have to take care of it). But I think it's a good idea, you can report an issue in the tracker if you want