Skip to content

Commit

Permalink
Adjust thankslist sorting, add more testing
Browse files Browse the repository at this point in the history
  • Loading branch information
rxu committed Oct 27, 2024
1 parent 8f97f33 commit 2976ae8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
5 changes: 3 additions & 2 deletions controller/thankslist.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,8 @@ public function main($mode, $author_id, $give)
$sort_key = $default_key;
}

$order_by .= $sort_key_sql[$sort_key] . ' ' . (($sort_dir == 'a') ? 'ASC' : 'DESC');
// Additionally order by u.username_clean ASC for possible equal other values
$order_by .= $sort_key_sql[$sort_key] . (($sort_dir == 'a') ? ' ASC' : ' DESC') . (($sort_key != 'a') ? ', u.username_clean ASC' : '');

// Build a relevant pagination_url
$params = [];
Expand Down Expand Up @@ -459,7 +460,7 @@ public function main($mode, $author_id, $give)
],
],
'ORDER_BY' => $order_by,
'GROUP_BY' => 't.' . $sortparam.'_id, u.user_id',
'GROUP_BY' => 't.' . $sortparam.'_id, u.username_clean',
];
}

Expand Down
45 changes: 36 additions & 9 deletions tests/functional/controller_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public function test_thanklist()
{
$this->create_user('user1');
$this->add_user_group('ADMINISTRATORS', ['user1']);

$this->create_user('user2');
$this->remove_user_group('NEWLY_REGISTERED', ['user2']);

$this->login('user1');

// Create 2 posts for user1
Expand All @@ -34,26 +38,46 @@ public function test_thanklist()

// Logout user1
$this->logout();

// Login as user2
$this->login('user2');

// Create thanks for every user1's post from user2
$crawler = self::request('GET', "viewtopic.php?f=2&t={$topic['topic_id']}&sid={$this->sid}");
$thanks_link1 = str_replace('./', '', html_entity_decode($crawler->filter('#lnk_thanks_post' . ((int) $post['post_id'] - 1))->attr('href')));
$thanks_link2 = str_replace('./', '', html_entity_decode($crawler->filter('#lnk_thanks_post' . $post['post_id'])->attr('href')));

// Create thanks for every user1's post from user2
self::request('GET', $thanks_link1);
self::request('GET', $thanks_link2);

// Logout user2
$this->logout();

// Login as admin
$this->login();

// Create a thank for every user1's post
// Create thanks for every user1's post from admin
$crawler = self::request('GET', "viewtopic.php?f=2&t={$topic['topic_id']}&sid={$this->sid}");
$thanks_link1 = str_replace('./', '', html_entity_decode($crawler->filter('#lnk_thanks_post' . ((int) $post['post_id'] - 1))->attr('href')));
self::request('GET', $thanks_link1);
$thanks_link2 = str_replace('./', '', html_entity_decode($crawler->filter('#lnk_thanks_post' . $post['post_id'])->attr('href')));
self::request('GET', $thanks_link1);
self::request('GET', $thanks_link2);

$this->add_lang_ext('gfksx/thanksforposts', 'thanks_mod');

// At this point:
// admin has: received thanks - 1, given thanks - 2
// user1 has: received thanks - 2, given thanks - 1
// user1 has: received thanks - 4, given thanks - 1
// user2 has: received thanks - 0, given thanks - 2
$crawler = self::request('GET', 'app.php/thankslist');
$this->assertStringContainsString($this->lang('THANKS_USER'), $crawler->filter('h2')->text());
$this->assertStringContainsString('2 users', $crawler->filter('div[class="pagination"]')->text());
$this->assertStringContainsString('user1', $crawler->filter('a[class="username"]')->text());
$this->assertStringContainsString('admin', $crawler->filter('a[class="username-coloured"]')->text());
$this->assertStringContainsString('3 users', $crawler->filter('div.pagination')->text());

// default sorting order is 'u.username_clean DESC'
$this->assertStringContainsString('user2', $crawler->filter('a.username')->eq(0)->text());
$this->assertStringContainsString('user1', $crawler->filter('a.username')->eq(1)->text());
$this->assertStringContainsString('admin', $crawler->filter('a.username-coloured')->text());
}

public function test_thanklist_sorting()
Expand All @@ -64,18 +88,21 @@ public function test_thanklist_sorting()

// Default sorting: username desc
$crawler = self::request('GET', 'app.php/thankslist');
$this->assertStringContainsString('user1', $crawler->filter('tbody')->filter('tr')->eq(0)->filter('td > a')->text());
$this->assertStringContainsString('admin', $crawler->filter('tbody')->filter('tr')->eq(1)->filter('td > a')->text());
$this->assertStringContainsString('user2', $crawler->filter('tbody')->filter('tr')->eq(0)->filter('td > a')->text());
$this->assertStringContainsString('user1', $crawler->filter('tbody')->filter('tr')->eq(1)->filter('td > a')->text());
$this->assertStringContainsString('admin', $crawler->filter('tbody')->filter('tr')->eq(2)->filter('td > a')->text());

// Sorting by `Has thanked` desc
$crawler = self::request('GET', 'app.php/thankslist?sk=f&sd=d');
$this->assertStringContainsString('admin', $crawler->filter('tbody')->filter('tr')->eq(0)->filter('td > a')->text());
$this->assertStringContainsString('user1', $crawler->filter('tbody')->filter('tr')->eq(1)->filter('td > a')->text());
$this->assertStringContainsString('user2', $crawler->filter('tbody')->filter('tr')->eq(1)->filter('td > a')->text());
$this->assertStringContainsString('user1', $crawler->filter('tbody')->filter('tr')->eq(2)->filter('td > a')->text());

// Sorting by `Has thanked` asc
$crawler = self::request('GET', 'app.php/thankslist?sk=f&sd=a');
$this->assertStringContainsString('user1', $crawler->filter('tbody')->filter('tr')->eq(0)->filter('td > a')->text());
$this->assertStringContainsString('admin', $crawler->filter('tbody')->filter('tr')->eq(1)->filter('td > a')->text());
$this->assertStringContainsString('user2', $crawler->filter('tbody')->filter('tr')->eq(2)->filter('td > a')->text());

// Sorting by `Been thanked` desc
$crawler = self::request('GET', 'app.php/thankslist?sk=e&sd=d');
Expand Down
10 changes: 5 additions & 5 deletions tests/functional/thanking_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function test_toplist_on_index()
$this->assertStringContainsString($this->lang('REPUT_TOPLIST', 5), $crawler->filter('div[class="stat-block thanks-list"] > h3')->text());

// From controller test:
// user1 has: received thanks - 2
// user1 has: received thanks - 4
// admin has: received thanks - 1
$this->assertEquals('user1', $crawler->filter('div[class="stat-block thanks-list"] > p > a')->eq(0)->text());
$this->assertEquals('admin', $crawler->filter('div[class="stat-block thanks-list"] > p > a')->eq(1)->text());
Expand All @@ -47,15 +47,15 @@ public function test_profile_info()
{
$this->login();

// Test if user profile info displayed
// Test if user1 profile info displayed
$this->add_lang_ext('gfksx/thanksforposts', 'thanks_mod');
$crawler = self::request('GET', "memberlist.php?mode=viewprofile&un=user1");

$this->assertStringContainsString($this->lang('GRATITUDES'), $crawler->filter('div[class="panel bg1"] > div > h3')->text());
$this->assertStringContainsString(html_entity_decode($this->lang('RECEIVED')) . ': 2 times', $crawler->filter('div[class="panel bg1"] > div > div[class="column2"] > dl > dt')->text());
$this->assertStringContainsString(html_entity_decode($this->lang('RECEIVED')) . ': 4 times', $crawler->filter('div[class="panel bg1"] > div > div[class="column2"] > dl > dt')->text());
$this->assertStringContainsString($this->lang('THANKS_LIST'), $crawler->filter('div[class="panel bg1"] > div > div[class="column2"] > dl > dd > a')->text());
$this->assertStringContainsString('./memberlist.php?mode=viewprofile&u=2', $crawler->filter('div[id="show_thanked"] > span > a')->attr('href'));
$this->assertStringContainsString('admin', $crawler->filter('div[id="show_thanked"] > span > a')->text());
$this->assertStringContainsString('./memberlist.php?mode=viewprofile&u=59', $crawler->filter('div[id="show_thanked"] > span > a')->attr('href'));
$this->assertStringContainsString('user2', $crawler->filter('div[id="show_thanked"] > span > a')->text());
$this->assertStringContainsString($this->lang('FOR_MESSAGE'), $crawler->filter('div[id="show_thanked"] > span > a')->eq(1)->text());
}

Expand Down

0 comments on commit 2976ae8

Please sign in to comment.