|
| 1 | +<?php |
| 2 | + |
| 3 | +use Webmention\Sender; |
| 4 | + |
| 5 | +class Test_Sender extends WP_UnitTestCase { |
| 6 | + private $post; |
| 7 | + |
| 8 | + public function setUp(): void { |
| 9 | + parent::setUp(); |
| 10 | + |
| 11 | + // Erstelle einen Test-Beitrag |
| 12 | + $this->post = self::factory()->post->create_and_get( array( |
| 13 | + 'post_content' => 'Test post with a link to <a href="https://example.com">Example</a>', |
| 14 | + ) ); |
| 15 | + } |
| 16 | + |
| 17 | + public function test_send_webmentions() { |
| 18 | + // Mock der Webmention-Endpunkt-Entdeckung |
| 19 | + add_filter( 'webmention_server_url', function( $url, $target ) { |
| 20 | + return 'https://example.com/webmention'; |
| 21 | + }, 10, 2 ); |
| 22 | + |
| 23 | + // Mock der HTTP-Anfrage |
| 24 | + add_filter( 'pre_http_request', function( $preempt, $args, $url ) { |
| 25 | + return array( |
| 26 | + 'response' => array( 'code' => 200 ), |
| 27 | + 'body' => 'Webmention received', |
| 28 | + ); |
| 29 | + }, 10, 3 ); |
| 30 | + |
| 31 | + $result = Sender::send_webmentions( $this->post->ID ); |
| 32 | + |
| 33 | + // Überprüfe, ob die Webmention gesendet wurde |
| 34 | + $this->assertIsArray( $result ); |
| 35 | + $this->assertContains( 'https://example.com', $result ); |
| 36 | + |
| 37 | + // Überprüfe, ob die URLs in den Post-Meta gespeichert wurden |
| 38 | + $mentioned_urls = get_post_meta( $this->post->ID, 'webmention_last_mentioned_urls', true ); |
| 39 | + $this->assertIsArray( $mentioned_urls ); |
| 40 | + $this->assertContains( 'https://example.com', $mentioned_urls ); |
| 41 | + } |
| 42 | + |
| 43 | + public function test_update_ping() { |
| 44 | + $pinged = array( |
| 45 | + 'https://example1.com', |
| 46 | + 'https://example2.com', |
| 47 | + ); |
| 48 | + |
| 49 | + $result = Sender::update_ping( $this->post->ID, $pinged ); |
| 50 | + |
| 51 | + // Überprüfe, ob die Pings aktualisiert wurden |
| 52 | + $this->assertIsString( $result ); |
| 53 | + $this->assertEquals( implode( "\n", $pinged ), $result ); |
| 54 | + |
| 55 | + // Überprüfe die Datenbank direkt |
| 56 | + $updated_post = get_post( $this->post->ID ); |
| 57 | + $this->assertEquals( $result, $updated_post->pinged ); |
| 58 | + } |
| 59 | + |
| 60 | + public function test_update_ping_invalid_post() { |
| 61 | + $result = Sender::update_ping( 999999, array( 'https://example.com' ) ); |
| 62 | + $this->assertFalse( $result ); |
| 63 | + } |
| 64 | + |
| 65 | + public function test_update_ping_invalid_pinged() { |
| 66 | + $result = Sender::update_ping( $this->post->ID, 'not an array' ); |
| 67 | + $this->assertFalse( $result ); |
| 68 | + } |
| 69 | + |
| 70 | + public function test_send_webmentions_with_error_response() { |
| 71 | + // Mock der Webmention-Endpunkt-Entdeckung |
| 72 | + add_filter( 'webmention_server_url', function( $url, $target ) { |
| 73 | + return 'https://example.com/webmention'; |
| 74 | + }, 10, 2 ); |
| 75 | + |
| 76 | + // Mock der HTTP-Anfrage mit 500er Fehler |
| 77 | + add_filter( 'pre_http_request', function( $preempt, $args, $url ) { |
| 78 | + return array( |
| 79 | + 'response' => array( |
| 80 | + 'code' => 500, |
| 81 | + 'message' => 'Internal Server Error', |
| 82 | + ), |
| 83 | + 'body' => 'Server Error', |
| 84 | + 'headers' => array(), |
| 85 | + 'cookies' => array(), |
| 86 | + ); |
| 87 | + }, 10, 3 ); |
| 88 | + |
| 89 | + $result = Sender::send_webmentions( $this->post->ID ); |
| 90 | + |
| 91 | + // Überprüfe, ob der Versuch neu geplant wurde |
| 92 | + $this->assertTrue( metadata_exists( 'post', $this->post->ID, '_mentionme' ) ); |
| 93 | + $this->assertEquals( '1', get_post_meta( $this->post->ID, '_mentionme_tries', true ) ); |
| 94 | + } |
| 95 | + |
| 96 | + public function tearDown(): void { |
| 97 | + parent::tearDown(); |
| 98 | + remove_all_filters( 'webmention_server_url' ); |
| 99 | + remove_all_filters( 'pre_http_request' ); |
| 100 | + } |
| 101 | +} |
0 commit comments