1

I am developing a plugin to simplify the addition of rewrites to Wordpress. I have started using the Wordpress test suite. Essentially I have a class RewriteManager to which you can add Rewrite objects. One of the methods for Rewrite is isThisRewrite() which queries itself to determine if the url called relates to this rewrite:

// add the rewrite to the manager
$manager->addRewrite(new Rewrite(array(
    "path" => "test/path/([^/]+)/([^/]+)/?$",
    "querystring" => "index.php?rewrite=test&test=$matches[1]&test2=$matches[2]"
);

// start the manager - takes care of adding the rules, query vars etc...
$manager->start();

What I am looking to do in my unit test is to add a rewrite rule, navigate to the relevant url and then query the query_vars to ensure that the rule has been added correctly. I have tried the following:

$this->manager->addRewrite($this->actionRewrite);
$this->manager->start();
flush_rewrite_rules();

$this->go_to("/test/path/hello/there");

At this point I expect $wp_query to show test and test2 to show relevantly populated query_vars but there are no query vars at all. Is there a specific way of testing custom rewrites?

0

There are two problems here, a conceptual and practical

Conceptional: What is it that you are testing? If you are testing that your function actually inserts the rule into the set of rewrite rules, than you should just inspect the generated rewrite rules array (from the option probably). Anything beyond that is not "unit testing" and more of "integration testing", something that have its place and can be done with phpunit, but it is not unit testing.

Practical: The wordpress unit test suit boots with ugly permalink setting, therefor no url parsing will happen at all. You will need to change the setting to any of the other permalink structures. You might also want to use full urls, as parsing might try to match the site url first.

1
  • Thanks for that and I get your point. I have been looking into Behat and Mink to achieve this. – Zakalwe Apr 8 '17 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.