Skip to content

Commit

Permalink
fix attachment issues and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pfefferle committed Jan 15, 2025
1 parent 1aeb486 commit 99a0a48
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 18 deletions.
30 changes: 16 additions & 14 deletions includes/class-shortcodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,24 +166,26 @@ public static function content( $atts, $content, $tag ) {
if ( empty( $content ) ) {
$content = get_post_meta( $item->ID, '_wp_attachment_image_alt', true );
}
} else {
$content = \get_post_field( 'post_content', $item );
}

if ( 'yes' === $atts['apply_filters'] ) {
/** This filter is documented in wp-includes/post-template.php */
$content = \apply_filters( 'the_content', $content );
} else {
$content = do_blocks( $content );
$content = wptexturize( $content );
$content = wp_filter_content_tags( $content );
}
if ( empty( $content ) ) {
$content = \get_post_field( 'post_content', $item );
}

// Replace script and style elements.
$content = \preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $content );
$content = strip_shortcodes( $content );
$content = \trim( \preg_replace( '/[\n\r\t]/', '', $content ) );
if ( 'yes' === $atts['apply_filters'] ) {
/** This filter is documented in wp-includes/post-template.php */
$content = \apply_filters( 'the_content', $content );
} else {
$content = do_blocks( $content );
$content = wptexturize( $content );
$content = wp_filter_content_tags( $content );
}

// Replace script and style elements.
$content = \preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $content );
$content = strip_shortcodes( $content );
$content = \trim( \preg_replace( '/[\n\r\t]/', '', $content ) );

add_shortcode( 'ap_content', array( 'Activitypub\Shortcodes', 'content' ) );

return $content;
Expand Down
8 changes: 4 additions & 4 deletions includes/transformer/class-attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ class Attachment extends Post {
* @return array The Attachments.
*/
protected function get_attachment() {
$mime_type = get_post_mime_type( $this->item->ID );
$media_type = preg_replace( '/(\/[a-zA-Z]+)/i', '', $mime_type );
$type = '';
$mime_type = \get_post_mime_type( $this->item->ID );
$mime_type_parts = \explode( '/', $mime_type );
$type = '';

switch ( $media_type ) {
switch ( $mime_type_parts[0] ) {
case 'audio':
case 'video':
$type = 'Document';
Expand Down
167 changes: 167 additions & 0 deletions tests/includes/transformer/class-test-attachment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<?php
/**
* Test file for Attachment transformer.
*
* @package ActivityPub
*/

namespace Activitypub\Tests\Transformer;

use Activitypub\Transformer\Attachment;
use WP_UnitTestCase;

/**
* Test class for Attachment Transformer.
*
* @coversDefaultClass \Activitypub\Transformer\Attachment
*/
class Test_Attachment extends WP_UnitTestCase {
/**
* Test attachment ID.
*
* @var int
*/
protected static $attachment_id;

/**
* Create fake data before tests run.
*
* @param WP_UnitTest_Factory $factory Helper that creates fake data.
*/
public static function wpSetUpBeforeClass( $factory ) {
// Create test attachment
self::$attachment_id = $factory->attachment->create_object(
array(
'post_type' => 'attachment',
'post_mime_type' => 'image/jpeg',
'post_title' => 'Test Image',
'post_content' => 'Test Image Description',
)
);
}

/**
* Clean up after tests.
*/
public static function wpTearDownAfterClass() {
wp_delete_post( self::$attachment_id, true );
}

/**
* Test get_type method.
*
* @covers ::get_type
*/
public function test_get_type() {
$attachment = get_post( self::$attachment_id );
$transformer = new Attachment( $attachment );
$type = $this->get_protected_method( $transformer, 'get_type' );

$this->assertEquals( 'Note', $type );
}

/**
* Test get_attachment method with different mime types.
*
* @covers ::get_attachment
* @dataProvider provide_mime_types
*/
public function test_get_attachment( $mime_type, $expected_type ) {
$attachment_id = self::factory()->attachment->create_object(
array(
'post_type' => 'attachment',
'post_mime_type' => $mime_type,
)
);

$attachment = get_post( $attachment_id );
$transformer = new Attachment( $attachment );
$result = $this->get_protected_method( $transformer, 'get_attachment' );

$this->assertIsArray( $result );
$this->assertEquals( $expected_type, $result['type'] );
$this->assertEquals( $mime_type, $result['mediaType'] );
$this->assertArrayHasKey( 'url', $result );

wp_delete_post( $attachment_id, true );
}

/**
* Test get_attachment method with alt text.
*
* @covers ::get_attachment
*/
public function test_get_attachment_with_alt() {
$alt_text = 'Test Alt Text';
update_post_meta( self::$attachment_id, '_wp_attachment_image_alt', $alt_text );

$attachment = get_post( self::$attachment_id );
$transformer = new Attachment( $attachment );
$result = $this->get_protected_method( $transformer, 'get_attachment' );

$this->assertArrayHasKey( 'name', $result );
$this->assertEquals( $alt_text, $result['name'] );
}

/**
* Test to_object method.
*
* @covers ::to_object
*/
public function test_to_object() {
$attachment = get_post( self::$attachment_id );
$transformer = new Attachment( $attachment );
$object = $transformer->to_object();

$this->assertEquals( 'Note', $object->get_type() );
$this->assertEquals( home_url( '?p=' . self::$attachment_id ), $object->get_id() );
$this->assertNull( $object->get_name() );
}

/**
* Data provider for mime types.
*
* @return array Test data.
*/
public function provide_mime_types() {
return array(
'image' => array(
'image/jpeg',
'Image',
),
'audio' => array(
'audio/mpeg',
'Document',
),
'video' => array(
'video/mp4',
'Document',
),
'pdf' => array(
'application/pdf',
'',
),
'text' => array(
'text/plain',
'',
),
);
}

/**
* Helper method to access protected methods.
*
* @param object $object Object instance.
* @param string $method_name Method name.
* @param array $parameters Optional parameters.
*
* @return mixed Method result.
*/
protected function get_protected_method( $object, $method_name, $parameters = array() ) {
$reflection = new \ReflectionClass( get_class( $object ) );
$method = $reflection->getMethod( $method_name );
$method->setAccessible( true );

return $method->invokeArgs( $object, $parameters );
}
}

0 comments on commit 99a0a48

Please sign in to comment.