Question about _self_test()

Question about _self_test()

by Diane Soini -
Number of replies: 3

I am upgrading a custom block in preparation for our upgrade to moodle 3.1 and ran it through codechecker until all errors were gone. I went to install it but ended up with an error pointing to line 849 of lib/upgradelib.php.

// OK, it's as we all hoped. For further tests, the object will do them itself.
if (!$blockobj->_self_test()) {
throw new plugin_defective_exception($component, 'Self test failed.');
}

Is it required that blocks define their own method? I put a function in the block code to simply return true, but I would like to know what I am really supposed to do with this method. I could not find anything more than a page Blocks/Appendix A that said this was a private method and no description is given. I did not see in the block development instructions to add this method. I'm new to moodle so sorry if I missed this somewhere.

Average of ratings: -
In reply to Diane Soini

Re: Question about _self_test()

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Maybe a good starting point would be to look at what the _self_test() function does:

https://github.com/moodle/moodle/blob/MOODLE_31_STABLE/blocks/moodleblock.class.php#L304

(Any good IDE could have found that function very quickly).

There are 3 checks in there:

  1. Does your block set the title field?
  2. Does you block set a content type? (if it inherits from a standard block base class it already does this)
  3. Does the block have at least one applicable format? (if you haven't overridden the standard function in a very bad way, it will pass this)

Overriding this function will simply cover up the problem, rather than fixing it.

In reply to Davo Smith

Re: Question about _self_test()

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

To add to Davo's comment, I recommend NetBeans (which is free) for the way it allows you to click on a function and jump to the source code. Other IDE's are available, e.g. eclipse and PHPStorm.

In reply to Davo Smith

Re: Question about _self_test()

by Diane Soini -

Thank you. The comment suggests that my code passed all the tests so I was a bit confused. And the block works fine, allows you to place it in reasonable places. One thing I could not find was a comprehensive list of applicable formats, or at least the standard ones that come with moodle. I wish moodle had better documentation. Fewer prosy words, just the facts.