@@ -310,6 +310,135 @@ public function testFromTermQuery()
310310 $ this ->assertTrue (MyTerm::class === get_class ($ res [1 ]));
311311 }
312312
313+ public function testFromTermQueryWithFields ()
314+ {
315+ $ term_ids = $ this ->factory ->term ->create_many (4 , [
316+ 'taxonomy ' => 'post_tag ' ,
317+ ]);
318+
319+ $ post_id = $ this ->factory ->post ->create ();
320+ wp_set_object_terms (
321+ $ post_id ,
322+ $ term_ids [0 ],
323+ 'post_tag '
324+ );
325+ wp_set_object_terms (
326+ $ this ->factory ->post ->create (),
327+ [$ term_ids [1 ], $ term_ids [2 ]],
328+ 'post_tag '
329+ );
330+
331+ $ termFactory = new TermFactory ();
332+
333+ // all: array of used terms as Timber\Term object
334+ $ termQuery = new WP_Term_Query ([
335+ 'taxonomy ' => 'post_tag ' ,
336+ 'fields ' => 'all ' ,
337+ ]);
338+ $ terms = $ termFactory ->from ($ termQuery );
339+ $ this ->assertCount (3 , $ terms );
340+ foreach ($ terms as $ term ) {
341+ $ this ->assertInstanceOf (Term::class, $ term );
342+ }
343+
344+ // all_with_object_id: all terms used in a specific object as Timber\Term object
345+ $ termQuery = new WP_Term_Query ([
346+ 'taxonomy ' => 'post_tag ' ,
347+ 'fields ' => 'all_with_object_id ' ,
348+ 'object_ids ' => $ post_id ,
349+ ]);
350+ $ terms = $ termFactory ->from ($ termQuery );
351+ $ this ->assertCount (1 , $ terms );
352+ $ this ->assertInstanceOf (Term::class, $ terms [0 ]);
353+ $ this ->assertSame ($ terms [0 ]->id , $ term_ids [0 ]);
354+
355+ // register class map and repeat previous tests
356+ $ my_class_map = function (array $ map ) {
357+ return array_merge ($ map , [
358+ 'post_tag ' => MyTerm::class,
359+ ]);
360+ };
361+ $ this ->add_filter_temporarily ('timber/term/classmap ' , $ my_class_map );
362+
363+ // all_with_object_id: all terms used in a specific object as MyTerm object
364+ $ termQuery = new WP_Term_Query ([
365+ 'taxonomy ' => 'post_tag ' ,
366+ 'fields ' => 'all ' ,
367+ ]);
368+ $ terms = $ termFactory ->from ($ termQuery );
369+ $ this ->assertCount (3 , $ terms );
370+ foreach ($ terms as $ term ) {
371+ $ this ->assertInstanceOf (MyTerm::class, $ term );
372+ }
373+
374+ // all_with_object_id: all terms used in a specific object as MyTerm object
375+ $ termQuery = new WP_Term_Query ([
376+ 'taxonomy ' => 'post_tag ' ,
377+ 'fields ' => 'all_with_object_id ' ,
378+ 'object_ids ' => $ post_id ,
379+ ]);
380+ $ terms = $ termFactory ->from ($ termQuery );
381+ $ this ->assertCount (1 , $ terms );
382+ $ this ->assertInstanceOf (MyTerm::class, $ terms [0 ]);
383+ $ this ->assertSame ($ terms [0 ]->id , $ term_ids [0 ]);
384+
385+ // count: number of used terms as integer string value
386+ $ termQuery = new WP_Term_Query ([
387+ 'taxonomy ' => 'post_tag ' ,
388+ 'fields ' => 'count ' ,
389+ ]);
390+ $ count = $ termFactory ->from ($ termQuery );
391+ $ this ->assertTrue (is_string ($ count ));
392+ $ this ->assertTrue (is_numeric ($ count ));
393+ $ this ->assertSame (intval ($ count ), 3 );
394+
395+ // count: number of terms as integer string value
396+ $ termQuery = new WP_Term_Query ([
397+ 'taxonomy ' => 'post_tag ' ,
398+ 'fields ' => 'count ' ,
399+ 'hide_empty ' => false ,
400+ ]);
401+ $ count = $ termFactory ->from ($ termQuery );
402+ $ this ->assertTrue (is_string ($ count ));
403+ $ this ->assertTrue (is_numeric ($ count ));
404+ $ this ->assertSame (intval ($ count ), 4 );
405+
406+ // ids: array of integer ids of used terms
407+ $ termQuery = new WP_Term_Query ([
408+ 'taxonomy ' => 'post_tag ' ,
409+ 'fields ' => 'ids ' ,
410+ ]);
411+ $ ids = $ termFactory ->from ($ termQuery );
412+ $ this ->assertCount (3 , $ ids );
413+ foreach ($ ids as $ id ) {
414+ $ this ->assertTrue (is_int ($ id ));
415+ $ this ->assertTrue (in_array ($ id , $ term_ids ));
416+ }
417+
418+ // names: array of strings
419+ $ termQuery = new WP_Term_Query ([
420+ 'taxonomy ' => 'post_tag ' ,
421+ 'fields ' => 'names ' ,
422+ ]);
423+ $ names = $ termFactory ->from ($ termQuery );
424+ $ this ->assertCount (3 , $ names );
425+ foreach ($ names as $ name ) {
426+ $ this ->assertTrue (is_string ($ name ));
427+ }
428+
429+ // id=>parent: array of numeric strings
430+ $ termQuery = new WP_Term_Query ([
431+ 'taxonomy ' => 'post_tag ' ,
432+ 'fields ' => 'id=>parent ' ,
433+ ]);
434+ $ map = $ termFactory ->from ($ termQuery );
435+ $ this ->assertCount (3 , $ map );
436+ foreach ($ map as $ k => $ v ) {
437+ $ this ->assertTrue (is_int ($ k ));
438+ $ this ->assertTrue (is_int (filter_var ($ v , FILTER_VALIDATE_INT )));
439+ }
440+ }
441+
313442 public function testFromAssortedArray ()
314443 {
315444 register_taxonomy ('make ' , 'post ' );
0 commit comments