I would like to know if there is a technique to test a hook without doing a whole process. Example with the hookActionObjectAddAfter I would like to test it without having to make a complete command as a client would do.
Is it possible ? There is unitary testing?
Thank you in advance
If you created a test.php file at the root of your site, put this code:
require(dirname(__FILE__).'/config/config.inc.php');
Hook::exec('actionObjectAddAfter', array( array('object' => $params)));
And in your browser, call the page yourdomain.tld/test.php
Regards
Related
I need to execute a function immediately when someone purchases a course or an order gets completed successfully. The function I am trying to execute is actually called an API. I don't see an appropriate Hook from LearnPress.
It works perfectly when I use "user_register"(when someone registers this hook fire) hook but it doesn't work when I use this "learn_press_confirm_order" hook given by LearnPress.
Do you guys know is there any appropriate way that I can follow and achieve this. Thank You for your time
First off, your Lime API key should be treated as a password - don't share it on the web! Go to your LimeLM Account right now, choose 'Settings', and choose 'Generate New Key'. I'll wait :)
There's nothing obviously wrong with your code, so I would debug it like this:
Put a die('setup'); immediately after the add_action. We want to be sure that this file is actually being called. If it is, remove the die.
Wordpress and LearnPress are fantastic, because you've got the source code. Go to wp-content/plugins/learnpress and type (on Linux or something *nix)
grep -R "learn_press_confirm_order" .
This will show you all the files that reference this action. There is only one:
./templates/order/confirm.php: transaction_method, $order->get_id() ); ?>
So pull up an editor and edit wp-content/plugins/learnpress/templates/order/confirm.php. You need to determine:
Whether the file is being run at all when you order. (Use die right at the top, or error_log if you can see your webserver/php log files.)
I'm fairly certain at this point you will have found the error, but there's a chance for some reason you're getting to this page, but the action isn't being called. So you might need to work out the exact flow of control on this confirm.php page. Again, die or error_log.
You can make live changes to the code of learnpress, to help you debug it. Most people are afraid to dig into other people's code, but that's the great power of open source. You can just reinstall learnpress when you're done.
Looking forward to hearing how it goes :)
I am new in wordpress developement, I have my own custom wordpress plugin which allows admin to click multiple author and save all meta tag in the database on post. It works fine. But i want to generate test case for that. I installed phpunit but I don't know how to write test case.
public function testOne()
{
$this->factory->post->create();
}
I tried this but not understand how it works.
It's not difficult but it's definitely not trivial. You'll need to set up a test Wordpress database just for PHPUnit to run tests
I found these guides really useful:
https://codesymphony.co/writing-wordpress-plugin-unit-tests/
https://engineering.hmn.md/guides/writing-code/writing-tests/
In order to get the files that PHPUnit needs to set up a WordPress testing environment, I had to get a new WordPress directory:
https://core.trac.wordpress.org/browser/trunk?order=name
And I was stymied for a while by MySQLi failing as soon as my unit tests started, but fixed it with a setting change after reading this:
https://core.trac.wordpress.org/ticket/39327
And now I can actually fix the bugs that the Unit Testing found :).
First of all, you should use the WordPress modules for Codeception, which includes the PHPunit WPTestCase and many other tools.
There are two basic approaches to testing something like this in WordPress:
You can test it with a browser. This is called an acceptance test. You list all the actions that prove the concept you are testing, and you run a browser [or browser simulator] to do the task step by step. You can look into the database for the artifacts you expect to see as a result, and prove it happens correctly. In your case, you might want to setup some posts, click the multiple authors, and then check the database for the meta tags you expect. It might look something like:
$I = /*am a */ new AcceptanceTester($scenario);
$I->loginAsAdmin();
$I->amOnPage("/wp-admin/post-new.php");
$I->fillField('#content', "lorum ipsum");
$I->click('#publish');
The other approach is to encapsulate your action in an OOP class, and test it using the WPUnit test tools. Codeception uses the same PHPUnit library the WordPress core teams uses, plus a bunch of other tools. The WPUnit approach loads a database into memory, and can do things like setup dummy posts for your plugin to work on. So in your case, you might have a class:
class SystemActionSaveMetaTags{
public function doSaveMetaTags()
}
You might have a test called
itShouldSaveMetaTags(){
$id = wp_insert_post();
$SystemActionSaveMetaTags = new SystemActionSaveMetaTags;
$SystemActionSaveMetaTags->doSaveMetaTags($id);
$this->assertTrue($this->checkForCorrectMetaTags($id));
}
Here is a tutorial on the subject, as it relates to WordPress:
https://wp-bdd.com/wp-codeception-tutorial/
I'm using PHPUnit/WP_UnitTestCase to unit test a WP theme. I need to simulate that I'm on a page with a certain slug, and I've been told to use go_to() to do this. However, I can't figure out how to actually use the function.
If I do something like this, it won't work:
$this->go_to('test');
$this->assertTrue(is_page('test'));
Any Advice?
The go_to function requires a url
https://core.trac.wordpress.org/browser/trunk/tests/phpunit/includes/testcase.php#L420
For the life of me I can't make durandaljs work with Areas. I'm developing an application with multiple mini SPAs, but I'm not sure how to set up durandaljs to work with it. I wasn't able to find anything online that can drive me in the right direction. The only similar question I found was this one, which is very vague.
My goal is to separate each SPA within it's own folder like so:
App
--areas
----area1
------viewmodels
------views
----area2
------viewmodels
------views
The router doesn't seem to have the concept of areas and no matter how I map the routes I get 404s when I call router.activate('page1'); after mapping with router.mapRoute('page1'); durandal is trying to get /App/viewmodels/page1.js.
Changing it to:
router.mapRoute('areas/area1/viewmodels/page1');
router.activate('areas/area1/viewmodels/page1');
results in another 404 fetching App/viewmodels/areas/area1/viewmodels/page1.js
I've also tried many other combinations which I no longer remember and can't seem to get it to work.
Can someone please post a working example of how to setup durandaljs with the router plugin and multiple mini SPAs (areas)? A link to an article document would also suffice.
You can use viewLocator.useConvention - maybe something like this:
viewLocator.useConvention(
"areas/area1/viewmodels",
"areas/area1/views",
"areas/area1/templates"
);
One good thing to realize is that useConvention() works in conjunction with any existing require.config paths setting. In other words, if you set the require.config so that "viewModels" and "views" are mapped to the right folders, then all is well.
For example, the code snippet above is functionally equivalent to:
window.require.config({
paths: {
"viewModels": "areas/area1/viewmodels",
"views": "areas/area1/views",
"templates": "areas/area1/templates"
}
viewLocator.useConvention("viewmodels", "views", "templates");
I a similar structure implemented in my application. I think that you have to put this piece of code, to do the viewLocator works properly.
viewLocator.useConvention(); //You can do that in you main.js
You can see more information here: http://durandaljs.com/documentation/View-Locator/
Also I recommed you to look the code of viewLocator.js, especially, the code of useConventionMethod.
Other possibility is to override the method convertModuleIdToViewId, to make it works as you want. But I think that using useConvention methos is enought.
A bit of a followup from a previous question.
As I mentioned in that question, my overall goal is to call a Ruby script after ImageCache does its magic with generating thumbnails and whatnot.
Sebi's suggestion from this question involved using hook_nodeapi.
Sadly, my Drupal knowledge of creating modules and/or hacking into existing modules is pretty limited.
So, for this question:
Should I create my own module or attempt to modify the ImageCache module?
How do I go about getting the generated thumbnail path (from ImageCache) to pass into my Ruby script?
edit
I found this question searching through SO...
Is it possible to do something similar in the _imagecache_cache function that would do what I want?
ie
function _imagecache_cache($presetname, $path) {
...
...
// check if deriv exists... (file was created between apaches request handler and reaching this code)
// otherwise try to create the derivative.
if (file_exists($dst) || imagecache_build_derivative($preset['actions'], $src, $dst)) {
imagecache_transfer($dst);
// call ruby script here
call('MY RUBY SCRIPT');
}
Don't hack into imagecache, remember every time you hack core/contrib modules god kills a kitten ;)
You should create a module that invokes hook_nodeapi, look at the api documentation to find the correct entry point for your script, nodeapi works on various different levels of the node process so you have to pick the correct one for you (it should become clear when you check the link out) http://api.drupal.org/api/function/hook_nodeapi
You won't be able to call the function you've shown because it is private so you'll have to find another route.
You could try and build the path up manually, you should be able to pull out the filename of the uploaded file and then append it to the directory structure, ugly but it should work. e.g.
If the uploaded file is called test123.jpg then it should be in /files/imagecache/thumbnails/test123/jpg (or something similar).
Hope it helps.