Skip to content

Commit d66bb2f

Browse files
authored
Merge pull request #73 from bleech/TestRuns_new
Fixes
2 parents 5bb1c66 + ee11e68 commit d66bb2f

File tree

7 files changed

+29
-28
lines changed

7 files changed

+29
-28
lines changed

components/test-run-alerts/_style.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
padding: 0 20px;
3131
position: sticky;
3232
top: -0.1px;
33-
z-index: 1;
33+
z-index: 3;
3434
}
3535

3636
&-link {

includes/core/utilities/class-image-helpers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static function alert_image_aspect_ratio( $alert ) {
4545
* @return string
4646
*/
4747
public static function get_screenshot_url( $object, $type, $size = 'full' ) {
48-
$property = "${type}_screenshot_url";
48+
$property = "{$type}_screenshot_url";
4949

5050
if ( ! property_exists( $object, $property ) ) {
5151
return '';

includes/features/class-onboarding.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function get_onboardings() {
112112
'description' => wp_kses_post( __( 'Starting from tomorrow, your Test will <strong>run daily</strong>, ensuring consistent monitoring of your page.', 'visual-regression-tests' ) ),
113113
],
114114
[
115-
'element' => '.vrts_navigation_item a[href$="admin.php?page=vrts-settings"]',
115+
'element' => '.vrts-admin-header__navigation-link[href$="admin.php?page=vrts-settings"]',
116116
'title' => wp_kses_post( __( '🛠️ Fine-tune your setup', 'visual-regression-tests' ) ),
117117
'description' => wp_kses_post( __( 'Further customize your Test configuration and plugin settings for an optimized experience.', 'visual-regression-tests' ) ),
118118
],
@@ -137,7 +137,7 @@ public function get_onboardings() {
137137
[
138138
'side' => 'bottom',
139139
'align' => 'center',
140-
'element' => '.vrts_navigation_item a[href$="admin.php?page=vrts-runs"]',
140+
'element' => '.vrts-admin-header__navigation-link[href$="admin.php?page=vrts-runs"]',
141141
'title' => wp_kses_post( __( '🚀 Meet the new Runs!', 'visual-regression-tests' ) ),
142142
'description' => wp_kses_post( __( 'Alerts are now bundled into Runs. Get a single report for each daily test, manual test, API trigger, or new: <strong>WordPress & plugin update (Pro)</strong>!', 'visual-regression-tests' ) ),
143143
],

includes/list-tables/class-test-runs-list-table.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,6 @@ public function prepare_items() {
160160
];
161161

162162
$this->items = Test_Run::get_items( $args );
163-
$test_run_ids = wp_list_pluck( $this->items, 'id' );
164-
$alert_counts = [];
165-
foreach ( Alert::get_unread_count_by_test_run_ids( $test_run_ids ) as $alert_count ) {
166-
$alert_counts[ $alert_count->test_run_id ] = $alert_count->count;
167-
}
168-
foreach ( $this->items as $item ) {
169-
$item->alerts_count = $alert_counts[ $item->id ] ?? 0;
170-
}
171163

172164
$total_items = 0;
173165
if ( null !== $args['filter_status'] ) {
@@ -198,7 +190,7 @@ public function single_row( $item ) {
198190
class="<?php echo esc_attr( $classes ); ?>"
199191
data-test-run-id="<?php echo esc_attr( $item->id ); ?>"
200192
data-test-run-new="<?php echo esc_attr( $is_new_run ? 'true' : 'false' ); ?>"
201-
<?php echo $item->alerts_count > 0 ? 'data-has-alerts' : ''; ?>
193+
<?php echo $item->unread_alerts_count > 0 ? 'data-has-alerts' : ''; ?>
202194
>
203195
<?php $this->single_row_columns( $item ); ?>
204196
</tr>
@@ -324,11 +316,10 @@ public function column_trigger( $item ) {
324316
* @return string
325317
*/
326318
public function column_status( $item ) {
327-
$alerts_count = count( maybe_unserialize( $item->alerts ) ?? [] );
328319
$tests_count = count( maybe_unserialize( $item->tests ) ?? [] );
329-
if ( $alerts_count > 0 ) {
320+
if ( $item->alerts_count > 0 ) {
330321
$status_class = 'paused';
331-
$status_text = esc_html__( 'Changes detected ', 'visual-regression-tests' ) . sprintf( '(%s)', $alerts_count );
322+
$status_text = esc_html__( 'Changes detected ', 'visual-regression-tests' ) . sprintf( '(%s)', $item->alerts_count );
332323

333324
} else {
334325
$status_class = 'running';

includes/models/class-test-run.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static function get_items( $args = [], $return_count = false ) {
4242

4343
if ( isset( $args['filter_status'] ) && null !== $args['filter_status'] ) {
4444
if ( 'changes-detected' === $args['filter_status'] ) {
45-
$where .= ' AND alerts IS NOT NULL';
45+
$where .= ' AND alerts_count > 0';
4646
}
4747
}
4848

@@ -82,14 +82,18 @@ public static function get_items( $args = [], $return_count = false ) {
8282
runs.id,
8383
$run_title,
8484
runs.tests,
85-
runs.alerts,
85+
SUM( IF( alerts.id IS NOT NULL, 1, 0 ) ) as alerts_count,
86+
SUM( IF( alerts.id IS NOT NULL and alerts.alert_state = 0, 1, 0 ) ) as unread_alerts_count,
8687
runs.trigger,
8788
runs.trigger_notes,
8889
runs.trigger_meta,
8990
runs.started_at,
9091
runs.scheduled_at,
9192
runs.finished_at
9293
FROM $test_runs_table as runs
94+
LEFT JOIN $alerts_table as alerts
95+
ON runs.id = alerts.test_run_id
96+
GROUP BY runs.id
9397
) runs
9498
$where
9599
$orderby
@@ -372,7 +376,7 @@ public static function get_calculated_status( $test_run ) {
372376
$test_run = self::get_item( $test_run );
373377
}
374378

375-
$has_alerts = ! empty( maybe_unserialize( $test_run->alerts ) );
379+
$has_alerts = ( $test_run->alerts_count ?? 0 ) > 0;
376380

377381
if ( $has_alerts ) {
378382
return 'has-alerts';
@@ -440,7 +444,7 @@ public static function get_status_data( $test_run ) {
440444

441445
switch ( $test_run_status ) {
442446
case 'has-alerts':
443-
$alerts_count = count( maybe_unserialize( $test_run->alerts ) );
447+
$alerts_count = $test_run->alerts_count;
444448
$class = 'paused';
445449
$text = esc_html__( 'Changes detected', 'visual-regression-tests' );
446450
$instructions = Date_Time_Helpers::get_formatted_relative_date_time( $test_run->finished_at );

includes/services/class-test-run-service.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Vrts\Core\Utilities\Url_Helpers;
66
use Vrts\Features\Service;
77
use Vrts\Features\Subscription;
8+
use Vrts\Models\Alert;
89
use Vrts\Models\Test;
910
use Vrts\Models\Test_Run;
1011
use Vrts\Services\Email_Service;
@@ -23,7 +24,6 @@ public function update_run_from_api_data( $data ) {
2324
$test_run = Test_Run::get_by_service_test_run_id( $run_id );
2425

2526
$test_run_just_finished = false;
26-
$alert_ids = [];
2727

2828
if ( $test_run && empty( $test_run->finished_at ) && ! empty( $data['finished_at'] ) ) {
2929
$test_run_just_finished = true;
@@ -41,7 +41,6 @@ public function update_run_from_api_data( $data ) {
4141

4242
$test_run_id = $this->create_test_run( $data['run_id'], [
4343
'tests' => maybe_serialize( $test_ids ),
44-
'alerts' => ! empty( $alert_ids ) ? maybe_serialize( $alert_ids ) : null,
4544
'started_at' => $data['started_at'],
4645
'finished_at' => $data['finished_at'],
4746
'scheduled_at' => $data['scheduled_at'],

includes/tables/class-test-runs-table.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class Test_Runs_Table {
66

7-
const DB_VERSION = '1.0';
7+
const DB_VERSION = '1.1';
88
const TABLE_NAME = 'vrts_test_runs';
99

1010
/**
@@ -32,7 +32,6 @@ public static function install_table() {
3232
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
3333
service_test_run_id varchar(40),
3434
tests text,
35-
alerts text default NULL,
3635
`trigger` varchar(20),
3736
trigger_notes text,
3837
trigger_meta text default NULL,
@@ -106,7 +105,7 @@ public static function create_runs_from_alerts() {
106105
'permalink' => get_permalink( $alert->post_id ),
107106
],
108107
] ),
109-
'alerts' => maybe_serialize( [ $alert->id ] ),
108+
'alert_id' => $alert->id,
110109
'trigger' => 'legacy',
111110
'started_at' => $alert->finished_at,
112111
'finished_at' => $alert->finished_at,
@@ -117,13 +116,21 @@ public static function create_runs_from_alerts() {
117116
return "('" . implode( "','", array_map( 'esc_sql', $run ) ) . "')";
118117
}, $test_runs));
119118

119+
// add alert_id column to test_runs table.
120+
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.SchemaChange
121+
$wpdb->query( "ALTER TABLE {$runs_table} ADD COLUMN alert_id bigint(20) unsigned;" );
122+
120123
// insert all test runs with single query.
121124
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
122-
$wpdb->query( "INSERT INTO {$runs_table} (tests, alerts, `trigger`, started_at, finished_at) VALUES " . $test_runs_values . ';' );
125+
$wpdb->query( "INSERT INTO {$runs_table} (tests, alert_id, `trigger`, started_at, finished_at) VALUES " . $test_runs_values . ';' );
123126

124-
// update test_run_id in alerts table from newly created test runs based on alerts column.
127+
// update test_run_id in alerts table from newly created test runs based on alert_id column.
125128
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
126-
$wpdb->query( "UPDATE {$alerts_table} a JOIN {$runs_table} r ON r.alerts LIKE CONCAT('%\"', a.id, '\"%') SET a.test_run_id = r.id;" );
129+
$wpdb->query( "UPDATE {$alerts_table} a JOIN {$runs_table} r ON r.alert_id = a.id SET a.test_run_id = r.id;" );
130+
131+
// remove alert_id column from test_runs table.
132+
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.SchemaChange
133+
$wpdb->query( "ALTER TABLE {$runs_table} DROP COLUMN alert_id;" );
127134

128135
return true;
129136
}

0 commit comments

Comments
 (0)