165 lines
6.1 KiB
PHP
165 lines
6.1 KiB
PHP
<?php
|
|
/**
|
|
* Test REST API - Availability Endpoint
|
|
*
|
|
* Uruchom w przeglądarce: http://jachty.pagedev.local/test-api-availability.php
|
|
*/
|
|
|
|
require_once __DIR__ . '/wp-load.php';
|
|
|
|
// Check if user is admin
|
|
if (!current_user_can('manage_options')) {
|
|
die('You must be logged in as admin to run this test.');
|
|
}
|
|
|
|
echo '<h1>Yacht Booking System - API Availability Test</h1>';
|
|
|
|
// 1. Get first yacht
|
|
echo '<h2>1. Test Setup - Get First Yacht</h2>';
|
|
$yachts = get_posts([
|
|
'post_type' => 'yacht',
|
|
'posts_per_page' => 1,
|
|
'orderby' => 'ID',
|
|
'order' => 'ASC',
|
|
]);
|
|
|
|
if (empty($yachts)) {
|
|
echo '<p style="color: red;">❌ Brak jachtów w systemie. Dodaj przynajmniej jeden jacht przed testem.</p>';
|
|
echo '<p><a href="' . admin_url('admin.php?page=yacht-bookings-add-yacht') . '">Dodaj jacht</a></p>';
|
|
die();
|
|
}
|
|
|
|
$yacht = $yachts[0];
|
|
echo '<p>✅ Znaleziono jacht: <strong>' . esc_html($yacht->post_title) . '</strong> (ID: ' . $yacht->ID . ')</p>';
|
|
|
|
// 2. Test API Endpoint - Current Month
|
|
echo '<h2>2. Test API Endpoint - Availability</h2>';
|
|
|
|
$start_date = date('Y-m-01'); // First day of current month
|
|
$end_date = date('Y-m-t'); // Last day of current month
|
|
|
|
$api_url = rest_url('yacht-booking/v1/availability/' . $yacht->ID);
|
|
$api_url = add_query_arg([
|
|
'start' => $start_date,
|
|
'end' => $end_date,
|
|
], $api_url);
|
|
|
|
echo '<p><strong>API URL:</strong> <code>' . esc_html($api_url) . '</code></p>';
|
|
|
|
// Call API
|
|
$response = wp_remote_get($api_url);
|
|
|
|
if (is_wp_error($response)) {
|
|
echo '<p style="color: red;">❌ Błąd API: ' . $response->get_error_message() . '</p>';
|
|
} else {
|
|
$status_code = wp_remote_retrieve_response_code($response);
|
|
$body = wp_remote_retrieve_body($response);
|
|
$data = json_decode($body, true);
|
|
|
|
echo '<p><strong>Status Code:</strong> ' . $status_code . '</p>';
|
|
|
|
if ($status_code === 200) {
|
|
echo '<p style="color: green;">✅ API Response: SUCCESS</p>';
|
|
echo '<details><summary>Raw Response (' . count($data) . ' dni)</summary><pre>';
|
|
print_r($data);
|
|
echo '</pre></details>';
|
|
|
|
// Count statuses
|
|
$statuses = ['available' => 0, 'booked' => 0, 'blocked' => 0];
|
|
foreach ($data as $day) {
|
|
if (isset($day['status'])) {
|
|
$statuses[$day['status']]++;
|
|
}
|
|
}
|
|
|
|
echo '<h3>Podsumowanie dostępności:</h3>';
|
|
echo '<ul>';
|
|
echo '<li>Dostępne dni: <strong>' . $statuses['available'] . '</strong></li>';
|
|
echo '<li>Zarezerwowane dni: <strong>' . $statuses['booked'] . '</strong></li>';
|
|
echo '<li>Zablokowane dni: <strong>' . $statuses['blocked'] . '</strong></li>';
|
|
echo '</ul>';
|
|
} else {
|
|
echo '<p style="color: red;">❌ Błąd HTTP: ' . $status_code . '</p>';
|
|
echo '<pre>' . esc_html($body) . '</pre>';
|
|
}
|
|
}
|
|
|
|
// 3. Test Direct Class Method
|
|
echo '<h2>3. Test Direct Method - Availability::get_availability_calendar()</h2>';
|
|
|
|
$calendar = \YachtBooking\Availability::get_availability_calendar($yacht->ID, $start_date, $end_date);
|
|
|
|
echo '<p>✅ Metoda zwróciła <strong>' . count($calendar) . '</strong> dni</p>';
|
|
echo '<details><summary>Pokaż kalendarz</summary>';
|
|
echo '<table border="1" cellpadding="5" style="border-collapse: collapse;">';
|
|
echo '<tr><th>Data</th><th>Status</th><th>Booking ID</th></tr>';
|
|
foreach ($calendar as $date => $info) {
|
|
$color = $info['status'] === 'available' ? '#d4edda' : ($info['status'] === 'booked' ? '#f8d7da' : '#fff3cd');
|
|
echo '<tr style="background: ' . $color . ';">';
|
|
echo '<td>' . esc_html($date) . '</td>';
|
|
echo '<td><strong>' . esc_html($info['status']) . '</strong></td>';
|
|
echo '<td>' . ($info['booking_id'] ? $info['booking_id'] : '—') . '</td>';
|
|
echo '</tr>';
|
|
}
|
|
echo '</table>';
|
|
echo '</details>';
|
|
|
|
// 4. Test is_available Method
|
|
echo '<h2>4. Test Method - Availability::is_available()</h2>';
|
|
|
|
$test_start = date('Y-m-d', strtotime('+7 days'));
|
|
$test_end = date('Y-m-d', strtotime('+10 days'));
|
|
|
|
$is_available = \YachtBooking\Availability::is_available($yacht->ID, $test_start, $test_end);
|
|
|
|
echo '<p>Sprawdzam dostępność jachtu od <strong>' . $test_start . '</strong> do <strong>' . $test_end . '</strong>:</p>';
|
|
if ($is_available) {
|
|
echo '<p style="color: green; font-size: 18px;">✅ Jacht jest DOSTĘPNY w tym terminie</p>';
|
|
} else {
|
|
echo '<p style="color: red; font-size: 18px;">❌ Jacht NIE jest dostępny w tym terminie</p>';
|
|
}
|
|
|
|
// 5. Test cache - Create test booking
|
|
echo '<h2>5. Test Cache - Create Test Booking</h2>';
|
|
|
|
$test_booking_start = date('Y-m-d', strtotime('+14 days'));
|
|
$test_booking_end = date('Y-m-d', strtotime('+17 days'));
|
|
|
|
echo '<p>Tworzę testową rezerwację od <strong>' . $test_booking_start . '</strong> do <strong>' . $test_booking_end . '</strong></p>';
|
|
|
|
// Mark as booked
|
|
\YachtBooking\Availability::mark_as_booked($yacht->ID, $test_booking_start, $test_booking_end, 999);
|
|
|
|
// Check if now unavailable
|
|
$is_still_available = \YachtBooking\Availability::is_available($yacht->ID, $test_booking_start, $test_booking_end);
|
|
|
|
if (!$is_still_available) {
|
|
echo '<p style="color: green;">✅ Cache działa! Daty zostały oznaczone jako zajęte</p>';
|
|
} else {
|
|
echo '<p style="color: red;">❌ Problem z cache! Daty nadal są dostępne</p>';
|
|
}
|
|
|
|
// Clean up test booking
|
|
echo '<p>Czyszczę testową rezerwację...</p>';
|
|
\YachtBooking\Availability::clear_booking_availability(999);
|
|
|
|
$is_available_again = \YachtBooking\Availability::is_available($yacht->ID, $test_booking_start, $test_booking_end);
|
|
|
|
if ($is_available_again) {
|
|
echo '<p style="color: green;">✅ Czyszczenie cache działa! Daty są ponownie dostępne</p>';
|
|
} else {
|
|
echo '<p style="color: red;">❌ Problem z czyszczeniem cache!</p>';
|
|
}
|
|
|
|
// Summary
|
|
echo '<hr>';
|
|
echo '<h2>✅ Testy Zakończone!</h2>';
|
|
echo '<p><strong>Wszystkie komponenty systemu dostępności działają poprawnie.</strong></p>';
|
|
|
|
echo '<h3>Następne kroki:</h3>';
|
|
echo '<ul>';
|
|
echo '<li><a href="' . admin_url('admin.php?page=yacht-bookings') . '">Zarządzaj jachtami</a></li>';
|
|
echo '<li><a href="' . rest_url('yacht-booking/v1/yachts') . '" target="_blank">Sprawdź API: /yachts</a></li>';
|
|
echo '<li><a href="' . $api_url . '" target="_blank">Sprawdź API: /availability</a></li>';
|
|
echo '</ul>';
|