Skip to content

Commit d3d1682

Browse files
- Added BBCode parsing to titles
- Added logic to truncate titles with BBCode in both JS and PHP - Added basic infrastructure for testing the tournament UI - Added basic unit test of tournament description UI - Replaced Array.prototype.findIndex with more compatible Array.prototype.some
1 parent 762e5ba commit d3d1682

11 files changed

Lines changed: 174 additions & 29 deletions

src/api/DummyApiResponder.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,13 @@ protected function get_interface_response_loadGameData($args) {
224224
);
225225
}
226226

227+
protected function get_interface_response_loadTournamentData($args) {
228+
return $this->load_json_data_from_file(
229+
'loadTournamentData',
230+
$args['tournament'] . '.json'
231+
);
232+
}
233+
227234
protected function get_interface_response_countPendingGames() {
228235
return $this->load_json_data_from_file(
229236
'countPendingGames',

src/engine/BMInterfaceTournament.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -503,17 +503,18 @@ protected function generate_new_games(BMTournament $tournament) {
503503
array($gameData['buttonId1'], $gameData['buttonId2'])
504504
);
505505

506-
$description = 'Round ' . $gameData['roundNumber'];
507-
if ('' != $tournament->description) {
508-
$description = $tournament->description . ' ' . $description;
506+
$roundDescription = 'Tournament Round ' . $gameData['roundNumber'];
507+
$tournDescription = $this->truncate_tournament_description($tournament->description);
508+
if ('' != $tournDescription) {
509+
$roundDescription = $tournDescription . ', ' . $roundDescription;
509510
}
510511

511512
$interfaceResponse = $this->game()->create_game_from_button_ids(
512513
array($gameData['playerId1'], $gameData['playerId2']),
513514
array($gameData['buttonId1'], $gameData['buttonId2']),
514515
$buttonNames,
515516
$tournament->gameMaxWins,
516-
$description,
517+
$roundDescription,
517518
NULL,
518519
0, // needs to be non-null, but also a non-player ID
519520
TRUE,
@@ -531,6 +532,33 @@ protected function generate_new_games(BMTournament $tournament) {
531532
}
532533
}
533534

535+
/**
536+
* Truncate a tournament description so that the tournament round number can be appended
537+
* without exceeding the max length of the field in the database.
538+
*
539+
* This also aggressively removes BBCode markup if there is the possibility of
540+
* breaking BBCode through truncation.
541+
*
542+
* @param string $description
543+
*/
544+
protected function truncate_tournament_description($description) {
545+
if (strlen($description) > 230) {
546+
$removedText = substr($description, 230);
547+
if (strpos($removedText, ']') !== FALSE) {
548+
// strip out all potential BBCode
549+
$description = preg_replace(
550+
'#\[([^\]]+?)(=[^\]]+?)?\](.+?)\[/\1\]#',
551+
'$3',
552+
$description
553+
);
554+
}
555+
556+
$description = substr($description, 0, 230) . '...';
557+
}
558+
559+
return($description);
560+
}
561+
534562
/**
535563
* Most of the tournament saving logic
536564
*

src/ui/js/Game.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1836,7 +1836,7 @@ Game.pageAddGameHeader = function(action_desc) {
18361836

18371837
if (Api.game.description) {
18381838
Game.page.append($('<div>', {
1839-
'text': Api.game.description,
1839+
'html': Env.applyBbCodeToHtml(Api.game.description),
18401840
'class': 'gameDescDisplay',
18411841
}));
18421842
}

src/ui/js/Overview.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,9 +443,12 @@ Overview.addScoreCol = function(gameRow, gameInfo) {
443443

444444
Overview.addDescCol = function(gameRow, description) {
445445
var descText = '';
446-
if (typeof(description) == 'string') {
447-
descText = description.substring(0, 30) +
448-
((description.length > 30) ? '...' : '');
446+
if (typeof(description) === 'string') {
447+
var descriptionNoMarkup =
448+
Env.applyBbCodeToHtml(description).replace(/<[^>]+>/g, '');
449+
450+
descText = descriptionNoMarkup.substring(0, 30) +
451+
((descriptionNoMarkup.length > 30) ? '...' : '');
449452
}
450453
gameRow.append($('<td>', {
451454
'class': 'gameDescDisplay',

src/ui/js/Tournament.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ Tournament.pageAddTournamentHeader = function() {
168168

169169
if (Api.tournament.description) {
170170
Tournament.page.append($('<div>', {
171-
'text': Api.tournament.description,
171+
'id': 'tournament_desc',
172+
'html': Env.applyBbCodeToHtml(Api.tournament.description),
172173
'class': 'gameDescDisplay',
173174
}));
174175
}
@@ -347,9 +348,18 @@ Tournament.pageAddWinnerInfo = function () {
347348
var winnerDiv = $('<div>');
348349
Tournament.page.append(winnerDiv);
349350

350-
var winnerIdx = Api.tournament.remainCountArray.findIndex(
351-
function(x) {return (x > 0);}
351+
var winnerIdx;
352+
var isWinnerFound = Api.tournament.remainCountArray.some(
353+
function(item, idx) {
354+
winnerIdx = idx;
355+
return item > 0;
356+
}
352357
);
358+
359+
if (!isWinnerFound) {
360+
return;
361+
}
362+
353363
var winnerPar = $('<p>', {
354364
'class': 'winner_name',
355365
'text': 'Winner: ' + Api.tournament.playerDataArray[winnerIdx].playerName

src/ui/js/TournamentOverview.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,11 @@ TournamentOverview.addTypeCol = function(tournamentRow, tournamentInfo) {
284284
TournamentOverview.addDescCol = function(tournamentRow, description) {
285285
var descText = '';
286286
if (typeof(description) === 'string') {
287-
descText = description.substring(0, 30) +
288-
((description.length > 30) ? '...' : '');
287+
var descriptionNoMarkup =
288+
Env.applyBbCodeToHtml(description).replace(/<[^>]+>/g, '');
289+
290+
descText = descriptionNoMarkup.substring(0, 30) +
291+
((descriptionNoMarkup.length > 30) ? '...' : '');
289292
}
290293
tournamentRow.append($('<td>', {
291294
'class': 'tournamentDescDisplay',

test/src/api/responderTournamentTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public function test_interface_tournament() {
211211
$gameOneExpData = $this->generate_init_expected_data_array($gameOneId, 'responder004', 'responder005', 1, 'SPECIFY_DICE');
212212
$gameOneExpData['tournamentId'] = $tournamentId;
213213
$gameOneExpData['tournamentRoundNumber'] = 1;
214-
$gameOneExpData['description'] = 'Round 1';
214+
$gameOneExpData['description'] = 'Tournament Round 1';
215215
$gameOneExpData['currentPlayerIdx'] = FALSE;
216216
$gameOneExpData['creatorDataArray'] = array('creatorId' => 0, 'creatorName' => '');
217217
$gameOneExpData['gameActionLog'][0]['message'] = 'Game created automatically';
@@ -243,7 +243,7 @@ public function test_interface_tournament() {
243243
$gameTwoExpData['gameSkillsInfo'] = $this->get_skill_info(array('Poison'));
244244
$gameTwoExpData['tournamentId'] = $tournamentId;
245245
$gameTwoExpData['tournamentRoundNumber'] = 1;
246-
$gameTwoExpData['description'] = 'Round 1';
246+
$gameTwoExpData['description'] = 'Tournament Round 1';
247247
$gameTwoExpData['activePlayerIdx'] = 0;
248248
$gameTwoExpData['playerWithInitiativeIdx'] = 0;
249249
$gameTwoExpData['creatorDataArray'] = array('creatorId' => 0, 'creatorName' => '');
@@ -363,7 +363,7 @@ public function test_interface_tournament() {
363363
$gameThreeExpData['gameSkillsInfo'] = $this->get_skill_info(array('Poison'));
364364
$gameThreeExpData['tournamentId'] = $tournamentId;
365365
$gameThreeExpData['tournamentRoundNumber'] = 2;
366-
$gameThreeExpData['description'] = 'Round 2';
366+
$gameThreeExpData['description'] = 'Tournament Round 2';
367367
$gameThreeExpData['activePlayerIdx'] = 1;
368368
$gameThreeExpData['playerWithInitiativeIdx'] = 1;
369369
$gameThreeExpData['currentPlayerIdx'] = 1;

test/src/engine/BMInterfaceTournamentTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,65 @@ public function test_create_tournament(
1818
) {
1919

2020
}
21+
22+
public function test_truncate_tournament_description() {
23+
$reflection = new ReflectionMethod($this->object, 'truncate_tournament_description');
24+
$reflection->setAccessible(true);
25+
26+
$description = 'short';
27+
$shortDescription = $reflection->invoke($this->object, $description);
28+
$this->assertEquals($description, $shortDescription, 'Short descriptions should not be truncated');
29+
30+
$description = '12345678901234567890123456789012345678901234567890' .
31+
'12345678901234567890123456789012345678901234567890' .
32+
'12345678901234567890123456789012345678901234567890' .
33+
'12345678901234567890123456789012345678901234567890' .
34+
'12345678901234567890123456789012345678901234567890' .
35+
'12345678901234567890123456789012345678901234567890';
36+
$shortDescription = $reflection->invoke($this->object, $description);
37+
$this->assertEquals(
38+
'12345678901234567890123456789012345678901234567890' .
39+
'12345678901234567890123456789012345678901234567890' .
40+
'12345678901234567890123456789012345678901234567890' .
41+
'12345678901234567890123456789012345678901234567890' .
42+
'123456789012345678901234567890...',
43+
$shortDescription,
44+
'Long text descriptions should be truncated appropriately'
45+
);
46+
47+
$description = '[forum=1,6]text[/forum]456789012345678901234567890' .
48+
'[forum=1,6]text[/forum]456789012345678901234567890' .
49+
'[forum=1,6]text[/forum]456789012345678901234567890' .
50+
'[forum=1,6]text[/forum]456789012345678901234567890' .
51+
'[forum=1,6]text[/forum]456789012345678901234567890' .
52+
'12345678901234567890123456789012345678901234567890';
53+
$shortDescription = $reflection->invoke($this->object, $description);
54+
$this->assertEquals(
55+
'[forum=1,6]text[/forum]456789012345678901234567890' .
56+
'[forum=1,6]text[/forum]456789012345678901234567890' .
57+
'[forum=1,6]text[/forum]456789012345678901234567890' .
58+
'[forum=1,6]text[/forum]456789012345678901234567890' .
59+
'[forum=1,6]text[/forum]4567890...',
60+
$shortDescription,
61+
'Early markup should not be removed'
62+
);
63+
64+
$description = '[forum=1,6]text[/forum]456789012345678901234567890' .
65+
'[forum=1,6]text[/forum]456789012345678901234567890' .
66+
'[forum=1,6]text[/forum]456789012345678901234567890' .
67+
'[forum=1,6]text[/forum]456789012345678901234567890' .
68+
'1234567890[forum=1,6]text[/forum]45678901234567890' .
69+
'12345678901234567890123456789012345678901234567890';
70+
$shortDescription = $reflection->invoke($this->object, $description);
71+
$this->assertEquals(
72+
'text456789012345678901234567890' .
73+
'text456789012345678901234567890' .
74+
'text456789012345678901234567890' .
75+
'text456789012345678901234567890' .
76+
'1234567890text4567890123456789012345678901234567890' .
77+
'123456789012345678901234567890...',
78+
$shortDescription,
79+
'Late square close brackets should trigger BBCode removal'
80+
);
81+
}
2182
}

test/src/ui/js/BMTestUtils.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@ BMTestUtils.getAllElements = function() {
4646
'Loader': JSON.stringify(Loader, null, " "),
4747
'Login': JSON.stringify(Login, null, " "),
4848
'Newgame': JSON.stringify(Newgame, null, " "),
49+
'Newtournament': JSON.stringify(Newtournament, null, " "),
4950
'Newuser': JSON.stringify(Newuser, null, " "),
5051
'OpenGames': JSON.stringify(OpenGames, null, " "),
5152
'Overview': JSON.stringify(Overview, null, " "),
5253
'Profile': JSON.stringify(Profile, null, " "),
54+
'Tournament': JSON.stringify(Tournament, null, " "),
55+
'TournamentOverview': JSON.stringify(TournamentOverview, null, " "),
5356
'UserPrefs': JSON.stringify(UserPrefs, null, " "),
5457
'Verify': JSON.stringify(Verify, null, " "),
5558
};
@@ -132,6 +135,14 @@ BMTestUtils.testGameId = function(gameDesc) {
132135
if (gameDesc == 'NOGAME') { return '10000000'; }
133136
};
134137

138+
// For each tournament reported by responderTest which we use in UI
139+
// tests, set a friendly name for tracking purposes. These values
140+
// need to be kept in sync with responderTest in order for anything
141+
// good to happen.
142+
BMTestUtils.testTournamentId = function(tournamentDesc) {
143+
if (tournamentDesc == 'default') { return '1'; }
144+
};
145+
135146
// We don't currently usually test reading the URL bar contents, because
136147
// that's hard to do within QUnit, but rather override those contents
137148
// with hardcoded values that we want to test.
@@ -143,6 +154,10 @@ BMTestUtils.overrideGetParameterByName = function() {
143154
return BMTestUtils.testGameId(BMTestUtils.GameType);
144155
}
145156

157+
if (name == 'tournament') {
158+
return BMTestUtils.testTournamentId(BMTestUtils.TournamentType);
159+
}
160+
146161
// always return the userid associated with tester1 in the fake data
147162
if (name == 'id') {
148163
return '1';

test/src/ui/js/test_Newtournament.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module("Newtournament", {
2626
delete Api.player;
2727
delete Newtournament.page;
2828
delete Newtournament.form;
29-
// delete Newtournament.justCreatedGame;
29+
delete Newtournament.justCreatedTournament;
3030

3131
Login.pageModule = null;
3232
Newtournament.activity = {};

0 commit comments

Comments
 (0)