test_language_logic.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. // Test language switching logic without redirects
  3. session_start();
  4. require_once 'includes/config.php';
  5. echo "=== Testing Language Switching Logic ===\n\n";
  6. // Test URL parsing logic directly
  7. function testUrlParsing($testUrl) {
  8. echo "Testing URL: $testUrl\n";
  9. $parsedUrl = parse_url($testUrl);
  10. $redirectUrl = $parsedUrl['path'];
  11. if (isset($parsedUrl['query'])) {
  12. parse_str($parsedUrl['query'], $queryParams);
  13. unset($queryParams['lang']); // Remove lang parameter
  14. if (!empty($queryParams)) {
  15. $redirectUrl .= '?' . http_build_query($queryParams);
  16. }
  17. }
  18. echo "Parsed redirect URL: $redirectUrl\n\n";
  19. return $redirectUrl;
  20. }
  21. // Test various URL scenarios
  22. $testUrls = [
  23. '/index.php?lang=fi&category=test',
  24. '/index.php?category=test&lang=fi',
  25. '/index.php?lang=fi',
  26. '/index.php?category=test&page=2',
  27. '/index.php'
  28. ];
  29. foreach ($testUrls as $url) {
  30. testUrlParsing($url);
  31. }
  32. // Test session handling
  33. echo "=== Testing Session Handling ===\n";
  34. // Simulate language setting
  35. $_SESSION['language'] = 'fi';
  36. echo "Session language set to: " . $_SESSION['language'] . "\n";
  37. // Test translation initialization
  38. try {
  39. require_once 'includes/translation.php';
  40. // Clear GET to avoid redirect
  41. $_GET = array();
  42. $translation = Translation::getInstance();
  43. echo "Translation instance created successfully\n";
  44. echo "Current language: " . $translation->getCurrentLanguage() . "\n";
  45. echo "Supported languages: " . implode(', ', array_keys($translation->getSupportedLanguages())) . "\n";
  46. } catch (Exception $e) {
  47. echo "Error: " . $e->getMessage() . "\n";
  48. }
  49. echo "\n=== Language Switching Logic Test Complete ===\n";
  50. ?>