Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -569,15 +569,7 @@ public function executeQueryEvents( array $input ): array {
*/
public static function supportsIndexedWindowQuery(): bool {
global $wpdb;
if ( true !== $wpdb->is_mysql || ! ( $wpdb->dbh instanceof \mysqli ) || defined( 'SQLITE_DB_DROPIN_VERSION' ) ) {
return false;
}
foreach ( array( 'DB_ENGINE', 'DATABASE_TYPE' ) as $constant ) {
if ( defined( $constant ) && 'sqlite' === strtolower( (string) constant( $constant ) ) ) {
return false;
}
}
return true;
return true === $wpdb->is_mysql && $wpdb->dbh instanceof \mysqli && ! defined( 'SQLITE_DB_DROPIN_VERSION' ) && ! \DataMachine\Core\Database\BaseRepository::is_sqlite();
}

/**
Expand Down Expand Up @@ -934,9 +926,7 @@ public function buildMatchingTermCountSql( array $input, array $taxonomies ): st

$input = $this->applyVenueTierConstraint( $input );

$db_engine = defined( 'DB_ENGINE' ) ? strtolower( (string) constant( 'DB_ENGINE' ) ) : '';
$database_type = defined( 'DATABASE_TYPE' ) ? strtolower( (string) constant( 'DATABASE_TYPE' ) ) : '';
$taxonomies = array_values(
$taxonomies = array_values(
array_unique(
array_filter(
array_map( 'sanitize_key', $taxonomies ),
Expand All @@ -945,8 +935,7 @@ public function buildMatchingTermCountSql( array $input, array $taxonomies ): st
)
);
if (
'sqlite' === $db_engine
|| 'sqlite' === $database_type
\DataMachine\Core\Database\BaseRepository::is_sqlite()
|| true !== $wpdb->is_mysql
|| empty( $taxonomies )
|| 'publish' !== ( $input['status'] ?? 'publish' )
Expand Down Expand Up @@ -1179,12 +1168,9 @@ public function buildMatchingEventDateAggregateSql( array $input, string $start_
private function buildSelectiveTaxonomyAggregateSql( array $input, string $start_expression, string $end_expression ): string {
global $wpdb;

$db_engine = defined( 'DB_ENGINE' ) ? strtolower( (string) constant( 'DB_ENGINE' ) ) : '';
$database_type = defined( 'DATABASE_TYPE' ) ? strtolower( (string) constant( 'DATABASE_TYPE' ) ) : '';
$tax_filters = is_array( $input['tax_filters'] ?? null ) ? $input['tax_filters'] : array();
$tax_filters = is_array( $input['tax_filters'] ?? null ) ? $input['tax_filters'] : array();
if (
'sqlite' === $db_engine
|| 'sqlite' === $database_type
\DataMachine\Core\Database\BaseRepository::is_sqlite()
|| true !== $wpdb->is_mysql
|| 1 !== count( $tax_filters )
|| empty( reset( $tax_filters ) )
Expand Down
2 changes: 1 addition & 1 deletion plugins/data-machine-events/inc/Core/EventOwnership.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public static function inPipelineWrite(): bool {
* @return bool
*/
public static function isActingHuman(): bool {
if ( self::inPipelineWrite() || get_current_user_id() <= 0 || wp_doing_cron() || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
if ( self::inPipelineWrite() || get_current_user_id() <= 0 || wp_doing_cron() || \DataMachine\Core\Bootstrap\RuntimeEnvironment::is_wp_cli() ) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Removes fixtures that a test committed past the WordPress test transaction.
*
* Tests that run in autocommit mode, or that exercise code issuing its own
* START TRANSACTION/COMMIT, persist their posts and terms beyond the per-test
* ROLLBACK. Left behind, those rows inflate global counts in later tests.
*
* @package DataMachineEvents\Tests\Support
*/

namespace DataMachineEvents\Tests\Support;

trait CommittedFixtureCleanup {

/** @var array<int,int> */
private array $committed_cleanup_posts = array();

/** @var array<int,string> Term ID => taxonomy. */
private array $committed_cleanup_terms = array();

/** Record every post and term created during the test. Call from setUp(). */
protected function trackCommittedFixtures(): void {
$this->committed_cleanup_posts = array();
$this->committed_cleanup_terms = array();
add_action( 'wp_insert_post', array( $this, 'recordCommittedPost' ) );
add_action( 'created_term', array( $this, 'recordCommittedTerm' ), 10, 3 );
}

/** @internal Hook callback. */
public function recordCommittedPost( int $post_id ): void {
$this->committed_cleanup_posts[ $post_id ] = $post_id;
}

/** @internal Hook callback. */
public function recordCommittedTerm( int $term_id, int $tt_id, string $taxonomy ): void {
$this->committed_cleanup_terms[ $term_id ] = $taxonomy;
}

/**
* Roll back the open transaction, then delete and commit whatever survived.
* Call from tearDown() before parent::tearDown(), while taxonomies are registered.
*/
protected function deleteCommittedFixtures(): void {
global $wpdb;
remove_action( 'wp_insert_post', array( $this, 'recordCommittedPost' ) );
remove_action( 'created_term', array( $this, 'recordCommittedTerm' ), 10 );

$wpdb->query( 'ROLLBACK' );
foreach ( $this->committed_cleanup_posts as $post_id ) {
clean_post_cache( $post_id );
if ( get_post( $post_id ) ) {
wp_delete_post( $post_id, true );
}
}
foreach ( array_reverse( $this->committed_cleanup_terms, true ) as $term_id => $taxonomy ) {
clean_term_cache( $term_id, $taxonomy );
if ( taxonomy_exists( $taxonomy ) && term_exists( (int) $term_id, $taxonomy ) ) {
wp_delete_term( (int) $term_id, $taxonomy );
}
}
$wpdb->query( 'COMMIT' );
}
}
11 changes: 11 additions & 0 deletions plugins/data-machine-events/tests/Unit/EffectiveEndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,29 @@
/** Effective-end schema, predicate and transition regressions. */
namespace DataMachineEvents\Tests\Unit;

require_once dirname( __DIR__ ) . '/Support/CommittedFixtureCleanup.php';

use DataMachineEvents\Core\EventDatesTable;
use DataMachineEvents\Blocks\Calendar\Query\UpcomingFilter;
use DataMachineEvents\Blocks\Calendar\Cache\CalendarCache;
use DataMachineEvents\Blocks\Calendar\Cache\PageExpiry;

class EffectiveEndTest extends \WP_UnitTestCase {
// EventDatesTable::create_table() runs DDL, which implicitly commits fixtures.
use \DataMachineEvents\Tests\Support\CommittedFixtureCleanup;

public function setUp(): void {
parent::setUp();
$this->trackCommittedFixtures();
EventDatesTable::create_table();
CalendarCache::invalidate();
}

public function tearDown(): void {
$this->deleteCommittedFixtures();
parent::tearDown();
}

public function test_effective_end_is_backfilled_and_maintained_by_upsert(): void {
global $wpdb;
$table = EventDatesTable::table_name();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace DataMachineEvents\Tests\Unit;

require_once dirname( __DIR__ ) . '/Support/CommittedFixtureCleanup.php';

use WP_UnitTestCase;
use DataMachineEvents\Steps\Upsert\Events\EventTaxonomyAssigner;
use DataMachineEvents\Core\Event_Post_Type;
Expand All @@ -19,13 +21,16 @@

class EventTaxonomyAssignerTest extends WP_UnitTestCase {

use \DataMachineEvents\Tests\Support\CommittedFixtureCleanup;

private EventTaxonomyAssigner $assigner;

public function setUp(): void {
parent::setUp();
global $wpdb;
$wpdb->query( 'COMMIT' );
$wpdb->query( 'SET autocommit = 1' );
$this->trackCommittedFixtures();

if ( ! post_type_exists( 'data_machine_events' ) ) {
Event_Post_Type::register();
Expand Down Expand Up @@ -68,6 +73,7 @@ public function setUp(): void {
}

public function tearDown(): void {
$this->deleteCommittedFixtures();
global $wpdb;
$wpdb->query( 'SET autocommit = 0' );
$wpdb->query( 'START TRANSACTION' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace DataMachineEvents\Tests\Unit;

require_once dirname( __DIR__ ) . '/Support/CommittedFixtureCleanup.php';

use DataMachineEvents\Abilities\EventUpdateAbilities;
use DataMachineEvents\Core\Event_Post_Type;
use DataMachineEvents\Core\Venue_Taxonomy;
Expand All @@ -15,16 +17,20 @@

class EventUpdateAbilitiesTest extends WP_UnitTestCase {

use \DataMachineEvents\Tests\Support\CommittedFixtureCleanup;

private EventUpdateAbilities $ability;

public function setUp(): void {
parent::setUp();
$this->trackCommittedFixtures();

$this->registerEventObjects();
$this->ability = new EventUpdateAbilities();
}

public function tearDown(): void {
$this->deleteCommittedFixtures();
remove_all_filters( 'datamachine_events_update_source_event_permission' );
remove_all_filters( 'datamachine_events_before_event_update_persistence' );
remove_all_actions( 'datamachine_events_after_event_update_persistence' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,13 +344,12 @@ public function test_single_packet_survives_real_ai_parking_resumes_and_upsert()
array( '%s' )
);
$original_settings = get_option( 'datamachine_settings', array() );

\DataMachine\Core\PluginSettings::update( array( 'pipeline_ai_concurrency_limit' => 1 ) );
update_option(
'datamachine_settings',
array_merge(
$original_settings,
array(
'pipeline_ai_concurrency_limit' => 1,
'mode_models' => array(
'pipeline' => array(
'provider' => 'fake_provider',
Expand Down
Loading