Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Q: Using the test database as the DB for the WPBrowser module #209

Closed
jcabot opened this issue Jan 17, 2019 · 2 comments

Comments

Projects
None yet
2 participants
@jcabot
Copy link
Contributor

commented Jan 17, 2019

I have a tests database configured for the WPDB module in my acceptance.suite.yml. As part of an acceptance test, I call $I->havePostinDatabase which does insert the new post in the test database (if I look via phymyadmin before ending the test).

Next, I move to $I->amOnAdminPage('/') where I have a dashboard widget that displays the number of posts published today. When using $I->see , the value that pops up is the one in the "real" localhost suite, not the one corresponding to the test database. So, I guess that WPDB uses the tests db but WPBrowser uses the "real" one.

This may be the default behaviour (based on the discussion in #189 ) but if so, is there any way to configure codeception / WPBrowser to force the WP site to use the tests database during the test? Or is there a completely different way to do this?

  • My acceptance.suite.yml file in case it helps
    actor: AcceptanceTester
    modules:
    enabled:
    - WPDb
    - WPBrowser
    - \Helper\Acceptance
    config:
    WPDb:
    dsn: 'mysql:host=localhost;dbname=wptests'
    user: 'roottest'
    password: 'roottest'
    dump: 'tests/data/dump.sql'
    populate: true
    cleanup: false
    url: 'http://127.0.0.1/wordpress'
    urlReplacement: false
    tablePrefix: 'wp
    '
    waitlock: 0
    WPBrowser:
    url: 'http://localhost/wordpress'
    adminUsername: 'admin'
    adminPassword: 'admin'
    adminPath: '/wp-admin'
@lucatume

This comment has been minimized.

Copy link
Owner

commented Jan 18, 2019

From your configuration the WPDb module is using the roottest database; if that's not the database that's serving the WordPress installation at http://localhost/wordpress then: yes, the results will not be aligned as the WPDb module is modifying a database that's not being used by your stack to serve WordPress at http://localhost/wordpress.
This is a test site configuration issue, not a wp-browser one. Here's a list of some fixes:

  1. you can modify the wp-config.php file of the site served at http://localhost/wordpress to use the roottest database with define( 'DB_NAME', 'roottest' );.
  2. you can modify the WPDb module configuration to use the database that's being used to serve WordPress at http://localhost/wordpress using the database name from the DB_NAME constant.
  3. (what I suggest) you can inject smart database switching in your configuration file using the roottest database for acceptance suite tests and the default one for normal operations, see below.

Db switch in wp-config.php

  1. modify your acceptance suites _before methods, the equivalent of PHPUnit setUp method, to send a special HTTP header on each request:
    $I->haveHttpHeader('X-Requested-With', 'Codeception');
  2. in your test site wp-config.php file look for that HTTP header to set the database name:
    if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'Codeception'){
    	define('DB_NAME', 'roottest' );
    } else {
    	define('DB_NAME', 'wordpress'); 
    }

In any case: do not use a site with any data you care about for your tests.
Let me know if this helps.

@jcabot

This comment has been minimized.

Copy link
Contributor Author

commented Jan 18, 2019

Great. I was looking for a way to setup option 3. Thanks!

@jcabot jcabot closed this Jan 18, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.