Opened 9 years ago
Closed 9 years ago
#6540 closed defect (bug) (fixed)
Multisite unit tests and bp_blogs_record_existing_blogs()
Reported by: | imath | Owned by: | boonebgorges |
---|---|---|---|
Milestone: | 2.4 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Build/Test Tools | Keywords: | has-patch 2nd-opinion |
Cc: |
Description (last modified by )
Each time this function is used in unit tests, for a weird reason users and blogs are not reset between each tests causing a large amount of tests to fail:
- because the domain of a blog created using
$this->factory->blog->create()
already exists - because users created using
$this->factory->user->create()
are not reset.
I've noticed, they were already a hack into the BP_UnitTestCase->setUP
method that was removing user 1. This was ok since each time bp_blogs_record_existing_blogs()
was used only one user was created in the test. But as soon as you create more than one, you have the problem.
As i'm working on a new "split" of #6026 i need to test this function. I'm suggesting another work around as after 2 days on it, i still don't get what's happening.
To reproduce, simply test the bp_blogs_record_existing_blogs_unittest.patch
using :
phpunit --group bp_blogs_record_existing_blogs -c tests/phpunit/multisite.xml
=> test is ok
Now run the complete suite :
phpunit -c tests/phpunit/multisite.xml
=>Dang! a large amount of errors and failures.
If you apply 6540.patch
> no more errors or failures !
Attachments (4)
Change History (10)
#3
@
9 years ago
6540.2.patch is an attempt at a patch that cleans up fixtures in a more targeted way, but with minimal requirements for the dev writing tests. Two parts:
- Add a hook at the end of
bp_blogs_record_existing_blogs()
. - In
BP_UnitTestCase()
, when 'bp_blogs_recorded_existing_blogs' fires, set an "autocommitted" flag. DuringtearDown()
, if this flag has been set totrue
, manually delete user and blog data.
If need be, we could add additional content deletion to this block (like, say, groups). And set_autocommit_flag()
could be used for other hooks, if we determined that other functions in BP were triggering autocommits.
Thanks for the digging, imath.
The root of the problem is this. Test fixtures, such as users, are created inside of a MySQL transaction. The WP test suite sets
AUTOCOMMIT=0
, which means that all writes are logged and then rolled back duringtearDown()
('ROLLBACK;'
). However,bp_blogs_record_existing_blogs()
callsTRUNCATE
, andTRUNCATE
causes an implicit commit. As a result, any fixtures created beforebp_blogs_record_existing_blogs()
are committed to the database and not rolled back duringtearDown()
.There are a couple possible solutions.
DELETE
instead ofTRUNCATE
inbp_blogs_record_existing_blogs()
. This is, in fact, the way the function worked until [8585]. The commit message says that the purpose was to reset the auto-increment value. This reset does seem like a good thing, all things being equal, but it would be nice if johnjamesjacoby could chime in with more context.bp_blogs_record_existing_blogs()
so that they clean up after themselves.Option 3 seems a lot more precise to me than 2. I'm going to work on a patch.