Skip to content

Commit

Permalink
feat(TestRuns): show updates version or language
Browse files Browse the repository at this point in the history
  • Loading branch information
domtra committed Aug 30, 2024
1 parent 6ed666b commit fda02c5
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 4 deletions.
74 changes: 73 additions & 1 deletion includes/features/class-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,72 @@ public static function run_api_tests( $notes = '' ) {
* @param array $options Options.
*/
public static function run_upgrader_tests( $upgrader, $options ) {
self::run_tests( 'update', null, $options );
$updates = [];
if ($options['action'] == 'update') {
switch ($options['type']) {
case 'plugin':
foreach($options['plugins'] as $plugin) {
$plugin_data = get_plugin_data(WP_PLUGIN_DIR . '/' . $plugin);
$new_version = $plugin_data['Version'];
$name = $plugin_data['Name'];
$slug = dirname(plugin_basename($plugin));
$updates[] = [
'type' => 'plugin',
'name' => $name,
'slug' => $slug,
'version' => $new_version,
];
}
break;
case 'theme':
foreach($options['themes'] as $theme) {
$theme_data = wp_get_theme($theme);
$new_version = $theme_data->get('Version');
$name = $theme_data->get('Name');
$slug = $theme;
$updates[] = [
'type' => 'theme',
'name' => $name,
'slug' => $slug,
'version' => $new_version,
];
}
break;
case 'core':
$new_version = static::get_wp_version();
$updates[] = [
'type' => 'core',
'version' => $new_version,
];
break;
case 'translation':
$translations = $options['translations'] ?? [];
foreach ($translations as $translation) {
$type = $translation['type'];
$slug = $translation['slug'];
$language = $translation['language'];
if ($type === 'plugin') {
$plugin_data = get_plugin_data(WP_PLUGIN_DIR . '/' . $slug . '/' . $slug . '.php');
$name = $plugin_data['Name'];
} elseif ($type === 'theme') {
$theme_data = wp_get_theme($slug);
$name = $theme_data->get('Name');
} else {
$name = 'WordPress';
}
$updates[] = [
'type' => $type,
'name' => $name,
'slug' => $slug,
'language' => $language,
];
}
break;
}
}
if ( ! empty( $updates ) ) {
self::run_tests( 'update', null, $updates );
}
}

/**
Expand All @@ -56,4 +121,11 @@ private static function run_tests( $trigger, $trigger_notes, $trigger_meta = nul
'trigger_meta' => $trigger_meta,
] );
}

static function get_wp_version() {
return (function() {
require ABSPATH . WPINC . '/version.php';
return $wp_version;
})();
}
}
3 changes: 2 additions & 1 deletion includes/list-tables/class-test-runs-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,12 +406,13 @@ protected function test_run_details( $item ) {
*/
public function column_trigger( $item ) {
$trigger_title = Test_Run::get_trigger_title( $item );
$trigger_note = Test_Run::get_trigger_note( $item );

return sprintf(
'<span class="vrts-test-run-trigger vrts-test-run-trigger--%s">%s</span><p class="vrts-test-run-trigger-notes">%s</p>',
esc_attr( $item->trigger ),
esc_html( $trigger_title ),
esc_html( $item->trigger_notes )
$trigger_note
);
}

Expand Down
3 changes: 2 additions & 1 deletion includes/list-tables/class-test-runs-queue-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,13 @@ public function column_title( $item ) {
*/
public function column_trigger( $item ) {
$trigger_title = Test_Run::get_trigger_title( $item );
$trigger_note = Test_Run::get_trigger_note( $item );

return sprintf(
'<span class="vrts-test-run-trigger vrts-test-run-trigger--%s">%s</span><p class="vrts-test-run-trigger-notes">%s</p>',
esc_attr( $item->trigger ),
esc_html( $trigger_title ),
esc_html( $item->trigger_notes )
$trigger_note
);
}

Expand Down
4 changes: 4 additions & 0 deletions includes/models/class-alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,10 @@ public static function is_archived( $alert ) {
public static function get_unread_count_by_test_run_ids( $test_run_ids ) {
global $wpdb;

if ( empty( $test_run_ids ) ) {
return [];
}

if ( is_int($test_run_ids) || is_string($test_run_ids) ) {
$test_run_ids = [ $test_run_ids ];
}
Expand Down
43 changes: 43 additions & 0 deletions includes/models/class-test-run.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public static function get_items( $args = [], $return_count = false ) {
runs.alerts,
runs.trigger,
runs.trigger_notes,
runs.trigger_meta,
runs.started_at,
runs.scheduled_at,
runs.finished_at
Expand Down Expand Up @@ -503,4 +504,46 @@ public static function delete_by_service_test_run_id( $test_run_id ) {
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- It's ok.
return $wpdb->delete( $test_runs_table, [ 'service_test_run_id' => $test_run_id ] );
}

public static function get_trigger_note( $test_run ) {
if ( $test_run->trigger ?? null === 'update' ) {
$updates = maybe_unserialize( $test_run->trigger_meta ) ?? [];
$updates = array_merge(...$updates);
// print_r($updates);
$types = ['core', 'plugin', 'theme', 'translation'];
$trigger_notes = '';
foreach ($types as $type) {
$updates_by_type = array_filter($updates, function($update) use ($type) {
// var_dump($update);
return $update['type'] === $type;
});
if (empty($updates_by_type)) {
continue;
}
$trigger_notes .= sprintf( '<strong>%s:</strong> ', ucfirst( $type ) );
$trigger_notes .= implode(', ', array_map(function($update) use ($type) {
$updateName = $update['name'] ?? $update['slug'] ?? null;
if ($type === 'translation') {
return '\'' . $updateName . ' (' . $update['language'] . ')' . '\'';
} elseif ($type === 'core') {
return $update['version'] ?? $update['language'] ?? '-';
} else {
if ( $update['version'] ?? false ) {
return $updateName . ' (' . $update['version'] . ')';
} elseif ( $update['language'] ?? false ) {
return $updateName . ' (' . $update['language'] . ')';
} else {
return $updateName ?? '';
}
}
}, $updates_by_type));
if ( ! empty( $trigger_notes ) ) {
$trigger_notes .= '. ';
}
}
return $trigger_notes;
} else {
return $test_run->trigger_notes ?? '';
}
}
}
1 change: 1 addition & 0 deletions includes/services/class-test-run-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function update_run_from_api_data( $data ) {
'scheduled_at' => $data['scheduled_at'],
'trigger' => $data['trigger'],
'trigger_notes' => $data['trigger_notes'],
'trigger_meta' => maybe_serialize( $data['trigger_meta'] ),
], true);

if ( $test_run_just_finished && ! empty( $alert_ids ) ) {
Expand Down
3 changes: 2 additions & 1 deletion includes/tables/class-test-runs-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class Test_Runs_Table {

const DB_VERSION = '1.0';
const DB_VERSION = '1.1';
const TABLE_NAME = 'vrts_test_runs';

/**
Expand Down Expand Up @@ -34,6 +34,7 @@ public static function install_table() {
alerts text default NULL,
`trigger` varchar(20),
trigger_notes text,
trigger_meta text default NULL,
started_at datetime,
scheduled_at datetime,
finished_at datetime,
Expand Down

0 comments on commit fda02c5

Please sign in to comment.