@@ -73,4 +73,84 @@ public function the_content_provider() {
7373 array ( $ pre , $ pre ),
7474 );
7575 }
76+
77+ /**
78+ * Tests auto-converting hashtags to tags.
79+ *
80+ * @see https://github.com/Automattic/wordpress-activitypub/issues/955
81+ * @dataProvider hashtag_provider
82+ * @covers ::insert_post
83+ *
84+ * @param string $content The post content.
85+ * @param string $excerpt The post excerpt.
86+ * @param string[] $expected_tags The expected tags.
87+ * @param string $message The error message.
88+ */
89+ public function test_hashtag_conversion ( $ content , $ excerpt , $ expected_tags , $ message ) {
90+ $ post_id = $ this ->factory ->post ->create (
91+ array (
92+ 'post_content ' => $ content ,
93+ 'post_excerpt ' => $ excerpt ,
94+ )
95+ );
96+
97+ \Activitypub \Hashtag::insert_post ( $ post_id , get_post ( $ post_id ) );
98+ $ tags = wp_get_post_tags ( $ post_id , array ( 'fields ' => 'names ' ) );
99+
100+ foreach ( $ expected_tags as $ tag ) {
101+ $ this ->assertContains ( $ tag , $ tags , $ message );
102+ }
103+ }
104+
105+ /**
106+ * Data provider for hashtag tests.
107+ *
108+ * @return array[] The data.
109+ */
110+ public function hashtag_provider () {
111+ return array (
112+ 'basic_hashtags ' => array (
113+ 'Testing #php and #programming ' ,
114+ '' ,
115+ array ( 'php ' , 'programming ' ),
116+ 'Basic hashtags should be converted ' ,
117+ ),
118+ 'hashtags_in_attributes ' => array (
119+ '<div style="color: #fff">#validtag</div> ' ,
120+ '' ,
121+ array ( 'validtag ' ),
122+ 'Hashtags in HTML attributes should be ignored ' ,
123+ ),
124+ 'mixed_content ' => array (
125+ 'Color is #red <span style="color: #ff0000">#valid</span> #blue ' ,
126+ '' ,
127+ array ( 'red ' , 'blue ' , 'valid ' ),
128+ 'Should handle mixed content correctly ' ,
129+ ),
130+ 'hex_in_text ' => array (
131+ '<span style="color: #ff0000">#f00</span> #fff #000000 ' ,
132+ '' ,
133+ array ( 'f00 ' , 'fff ' , '000000 ' ),
134+ 'Hex colors in text should be converted ' ,
135+ ),
136+ 'excerpt_tags ' => array (
137+ '' ,
138+ 'Testing #excerpt with #tags ' ,
139+ array ( 'excerpt ' , 'tags ' ),
140+ 'Should process excerpt hashtags ' ,
141+ ),
142+ 'multiple_attributes ' => array (
143+ '<div data-color="#123" style="border: 1px solid #456">#valid</div> ' ,
144+ '' ,
145+ array ( 'valid ' ),
146+ 'Should ignore multiple attribute hashtags ' ,
147+ ),
148+ 'quotes_in_content ' => array (
149+ 'Here is a "#quoted" #tag ' ,
150+ '' ,
151+ array ( 'tag ' ),
152+ 'Should handle quotes in content correctly ' ,
153+ ),
154+ );
155+ }
76156}
0 commit comments