Skip to content

Commit 20a4717

Browse files
committed
Tests: Cover default tag-cloud count scaling.
Add coverage for `default_topic_count_scale()`, including zero, rounding, and numeric-string inputs. Verify that `wp_generate_tag_cloud()` uses logarithmic scaling for font sizes by default. Developed in: #14332 Props rejaulalomkhan. See #65819. git-svn-id: https://develop.svn.wordpress.org/trunk@64427 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 45d6c69 commit 20a4717

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
/**
4+
* Tests for default_topic_count_scale().
5+
*
6+
* @group taxonomy
7+
*
8+
* @covers ::default_topic_count_scale
9+
*/
10+
class Tests_Term_DefaultTopicCountScale extends WP_UnitTestCase {
11+
12+
/**
13+
* Tests that a count is scaled logarithmically.
14+
*
15+
* @ticket 65819
16+
*
17+
* @dataProvider data_counts
18+
*
19+
* @param int|string $count Number of posts with the tag.
20+
* @param int $expected Expected scaled count.
21+
*/
22+
public function test_scales_count_logarithmically( $count, $expected ) {
23+
$this->assertSame( $expected, default_topic_count_scale( $count ) );
24+
}
25+
26+
/**
27+
* Data provider.
28+
*
29+
* @return array<string, array{0: int|string, 1: int}>
30+
*/
31+
public static function data_counts() {
32+
return array(
33+
'zero' => array( 0, 0 ),
34+
'one' => array( 1, 30 ),
35+
'rounds down' => array( 2, 48 ),
36+
'rounds up' => array( 4, 70 ),
37+
'nine' => array( 9, 100 ),
38+
'ninety-nine' => array( 99, 200 ),
39+
'nine hundred ninety' => array( 999, 300 ),
40+
'numeric string' => array( '9', 100 ),
41+
);
42+
}
43+
44+
/**
45+
* Tests that wp_generate_tag_cloud() uses this scale for font sizes by default.
46+
*
47+
* With counts of 1, 4 and 9 the scaled counts are 30, 70 and 100, so the middle tag
48+
* gets 16pt between 8pt and 22pt. A linear scale would give it 13.25pt.
49+
*
50+
* @ticket 65819
51+
*
52+
* @covers ::wp_generate_tag_cloud
53+
*/
54+
public function test_tag_cloud_uses_the_scale_by_default() {
55+
$tags = array();
56+
foreach ( array( 1, 4, 9 ) as $count ) {
57+
$tag = get_term( self::factory()->tag->create( array( 'name' => "Count $count" ) ), 'post_tag' );
58+
$tag->count = $count;
59+
$tag->link = get_term_link( $tag );
60+
$tags[] = $tag;
61+
}
62+
63+
$links = wp_generate_tag_cloud(
64+
$tags,
65+
array(
66+
'smallest' => 8,
67+
'largest' => 22,
68+
'unit' => 'pt',
69+
'orderby' => 'count',
70+
'format' => 'array',
71+
)
72+
);
73+
74+
$this->assertCount( 3, $links, 'There should be one link per tag.' );
75+
$this->assertStringContainsString( 'font-size: 8pt;', $links[0], 'The least used tag should get the smallest size.' );
76+
$this->assertStringContainsString( 'font-size: 16pt;', $links[1], 'The middle tag should be sized by the logarithmic scale.' );
77+
$this->assertStringContainsString( 'font-size: 22pt;', $links[2], 'The most used tag should get the largest size.' );
78+
}
79+
}

0 commit comments

Comments
 (0)