Skip to content

Commit bf6dc87

Browse files
committed
Add unit tests for the function used to seporate classnames
1 parent f592e27 commit bf6dc87

File tree

1 file changed

+293
-2
lines changed

1 file changed

+293
-2
lines changed

tests/test-navwalker.php

Lines changed: 293 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,47 @@ function setUp() {
3131
'container_class' => 'a_container_class',
3232
'menu_class' => 'a_menu_class',
3333
'menu_id' => 'a_menu_id',
34-
'echo' => true,
34+
'echo' => true,
3535
);
3636

37-
// array of the possible linkmods.
37+
// array of the possible linkmod typeflags.
3838
$this->valid_linkmod_typeflags = array(
3939
'dropdown-header',
4040
'dropdown-divider',
4141
);
42+
43+
// array of all possible linkmods, including the valid typeflags.
44+
$this->valid_linkmod_classes = array_merge( $this->valid_linkmod_typeflags, array(
45+
'disabled',
46+
'sr-only',
47+
) );
48+
49+
// array of valid font-awesome icon class starters plus some randomly chosen icon classes.
50+
$this->some_fontawesome_classes = array(
51+
'fa',
52+
'fas',
53+
'fab',
54+
'far',
55+
'fal',
56+
'fa-android',
57+
'fa-css3',
58+
'fa-home',
59+
'fa-bluetooth-b',
60+
'fa-chess-rook',
61+
);
62+
63+
// array of valid glyphicon icon class starters plus some randomly chosen icon classes.
64+
$this->some_glyphicons_classes = array(
65+
'glyphicon',
66+
'glyphicon-asterisk',
67+
'glyphicon-ok',
68+
'glyphicon-file',
69+
'glyphicon-hand-left',
70+
'glyphicon-sd-video',
71+
'glyphicon-subscript',
72+
'glyphicon-grain',
73+
);
74+
4275
}
4376

4477
/**
@@ -124,6 +157,9 @@ function test_fallback_function_exists() {
124157
*
125158
* Expects that for logged out users both echo and return requests should
126159
* produce empty strings.
160+
*
161+
* @access public
162+
* @return void
127163
*/
128164
function test_fallback_function_output_loggedout() {
129165

@@ -156,6 +192,9 @@ function test_fallback_function_output_loggedout() {
156192
*
157193
* Expects strings to be produced with html markup and that they match when
158194
* requesting either a return or defaulting to echo.
195+
*
196+
* @access public
197+
* @return void
159198
*/
160199
function test_fallback_function_output_loggedin() {
161200

@@ -206,6 +245,251 @@ function test_seporate_linkmods_and_icons_from_classes_function_exists() {
206245

207246
}
208247

248+
/**
249+
* Test that the function catches a random assortment of glyphicon icon
250+
* classes mixed with with regular classnames.
251+
*
252+
* @depends test_seporate_linkmods_and_icons_from_classes_function_exists
253+
*
254+
* @access public
255+
* @return void
256+
*/
257+
function test_seporate_linkmods_and_icons_from_classes_fontawesome() {
258+
259+
$wp_bootstrap_navwalker = $this->walker;
260+
// since we're working with private methods we need to use a reflector.
261+
$reflector = new ReflectionClass( 'WP_Bootstrap_Navwalker' );
262+
263+
// get a reflected method for the opener function and set to public.
264+
$method_open = $reflector->getMethod( 'seporate_linkmods_and_icons_from_classes' );
265+
$method_open->setAccessible( true );
266+
267+
$icons_array = $this->some_fontawesome_classes;
268+
$linkmod_classes = array();
269+
$icon_classes = array();
270+
$chars = range( 'a', 'z' );
271+
$extra_classes = array();
272+
// make 10 random valid classnames with legth of 8 chars (there should
273+
// be no naming collisions here with this random string gen method).
274+
for ( $i = 0; $i < 20; $i++ ) {
275+
$string = '';
276+
$length = mt_rand( 4, 10 );
277+
for ( $j = 0; $j < $length; $j++ ) {
278+
$string .= $chars[ mt_rand( 0, count( $chars ) - 1 ) ];
279+
}
280+
$extra_classes[] = $string;
281+
}
282+
// merge the valid icon classes with the extra classes and shuffle order.
283+
$icons_array = array_merge( $icons_array, $extra_classes );
284+
shuffle( $icons_array );
285+
286+
$returned_array = $method_open->invokeArgs( $wp_bootstrap_navwalker, array( $icons_array, &$linkmod_classes, &$icon_classes, 0 ) );
287+
288+
// linkmod_classes should be empty and returned_array should not.
289+
$this->assertTrue( ( empty( $linkmod_classes ) && ! empty( $returned_array ) ) );
290+
// icon_classes should no longer be empty.
291+
$this->assertNotTrue( empty( $icon_classes ) );
292+
// the number of items inside updated $icon_classes should match number of valids we started with.
293+
$this->assertTrue( count( $this->some_fontawesome_classes ) === count( $icon_classes ), "Seems that glyphicons classes are not catptured properly... \nvalid: \n" . print_r( $this->some_fontawesome_classes, true ) . "\nreturned: \n" . print_r( $icon_classes, true ) );
294+
// get the differences between the original classes and updated classes.
295+
$icon_differences = array_diff( $this->some_fontawesome_classes, $icon_classes );
296+
// should be no differences thus empty array, this being TRUE also means
297+
// that text was exact match in the updated array vs the original.
298+
$this->assertTrue( empty( $icon_differences ) );
299+
300+
}
301+
302+
/**
303+
* Test that the function catches a random assortment of font awesome icon
304+
* classes mixed with with regular classnames.
305+
*
306+
* @depends test_seporate_linkmods_and_icons_from_classes_function_exists
307+
*
308+
* @access public
309+
* @return void
310+
*/
311+
function test_seporate_linkmods_and_icons_from_classes_glyphicons() {
312+
313+
$wp_bootstrap_navwalker = $this->walker;
314+
// since we're working with private methods we need to use a reflector.
315+
$reflector = new ReflectionClass( 'WP_Bootstrap_Navwalker' );
316+
317+
// get a reflected method for the opener function and set to public.
318+
$method_open = $reflector->getMethod( 'seporate_linkmods_and_icons_from_classes' );
319+
$method_open->setAccessible( true );
320+
321+
$icons_array = $this->some_glyphicons_classes;
322+
$linkmod_classes = array();
323+
$icon_classes = array();
324+
$chars = range( 'a', 'z' );
325+
$extra_classes = array();
326+
// make 10 random valid classnames with legth of 8 chars (there should
327+
// be no naming collisions here with this random string gen method).
328+
for ( $i = 0; $i < 10; $i++ ) {
329+
$string = '';
330+
$length = mt_rand( 4, 10 );
331+
for ( $j = 0; $j < $length; $j++ ) {
332+
$string .= $chars[ mt_rand( 0, count( $chars ) - 1 ) ];
333+
}
334+
$extra_classes[] = $string;
335+
}
336+
// merge the valid icon classes with the extra classes and shuffle order.
337+
$icons_array = array_merge( $icons_array, $extra_classes );
338+
shuffle( $icons_array );
339+
340+
$returned_array = $method_open->invokeArgs( $wp_bootstrap_navwalker, array( $icons_array, &$linkmod_classes, &$icon_classes, 0 ) );
341+
342+
// linkmod_classes should be empty and returned_array should not.
343+
$this->assertTrue( ( empty( $linkmod_classes ) && ! empty( $returned_array ) ) );
344+
// icon_classes should no longer be empty.
345+
$this->assertNotTrue( empty( $icon_classes ) );
346+
// the number of items inside updated $icon_classes should match number of valids we started with.
347+
$this->assertTrue( count( $this->some_glyphicons_classes ) === count( $icon_classes ), "Seems that glyphicons classes are not catptured properly... \nvalid: \n" . print_r( $this->some_glyphicons_classes, true ) . "\nreturned: \n" . print_r( $icon_classes, true ) );
348+
// get the differences between the original classes and updated classes.
349+
$icon_differences = array_diff( $this->some_glyphicons_classes, $icon_classes );
350+
// should be no differences thus empty array, this being TRUE also means
351+
// that text was exact match in the updated array vs the original.
352+
$this->assertTrue( empty( $icon_differences ) );
353+
354+
}
355+
356+
/**
357+
* Test that the function catches a random assortment of font awesome icon
358+
* classes mixed with with regular classnames.
359+
*
360+
* @depends test_seporate_linkmods_and_icons_from_classes_function_exists
361+
*
362+
* @access public
363+
* @return void
364+
*/
365+
function test_seporate_linkmods_and_icons_from_classes_linkmods() {
366+
367+
$wp_bootstrap_navwalker = $this->walker;
368+
// since we're working with private methods we need to use a reflector.
369+
$reflector = new ReflectionClass( 'WP_Bootstrap_Navwalker' );
370+
371+
// get a reflected method for the opener function and set to public.
372+
$method_open = $reflector->getMethod( 'seporate_linkmods_and_icons_from_classes' );
373+
$method_open->setAccessible( true );
374+
375+
$valid_linkmods = $this->valid_linkmod_classes;
376+
$linkmod_classes = array();
377+
$icon_classes = array();
378+
$chars = range( 'a', 'z' );
379+
$extra_classes = array();
380+
// make 20 random valid classnames with legth of 4 to 10 chars. There
381+
// should be no naming collisions here with this random string gen.
382+
for ( $i = 0; $i < 10; $i++ ) {
383+
$string = '';
384+
$length = mt_rand( 4, 10 );
385+
for ( $j = 0; $j < $length; $j++ ) {
386+
$string .= $chars[ mt_rand( 0, count( $chars ) - 1 ) ];
387+
}
388+
$extra_classes[] = $string;
389+
}
390+
// merge the valid icon classes with the extra classes and shuffle order.
391+
$linkmod_array = array_merge( $valid_linkmods, $extra_classes );
392+
shuffle( $linkmod_array );
393+
394+
// NOTE: this is depth of 0 and meaning valid_linkmod_typeflags won't be captured.
395+
$returned_array = $method_open->invokeArgs( $wp_bootstrap_navwalker, array( $linkmod_array, &$linkmod_classes, &$icon_classes, 0 ) );
396+
397+
// linkmod_classes should NOT be empty and returned_array should not.
398+
$this->assertTrue( ( ! empty( $linkmod_classes ) && ! empty( $returned_array ) ) );
399+
// icon_classes should be empty.
400+
$this->assertTrue( empty( $icon_classes ) );
401+
402+
// the number of items inside updated array should match [what we started with - minus the linkmods for inside dropdowns].
403+
$this->assertTrue(
404+
(bool) count( ( $this->valid_linkmod_classes ) === ( count( $linkmod_classes ) - count( $this->valid_linkmod_typeflags ) ) ),
405+
"Seems that the linkmod classes are not catptured properly when outside of dropdowns... \nvalid: \n" . print_r( $this->valid_linkmod_classes, true ) . "\nreturned: \n" . print_r( $linkmod_classes, true ) . "\n" . count( $this->valid_linkmod_classes ) . ' ' . count( $linkmod_classes ) . ' ' . count( $this->valid_linkmod_typeflags ) );
406+
// get the differences between the original classes and updated classes.
407+
$linkmod_differences = array_diff( $this->valid_linkmod_classes, $linkmod_classes, $this->valid_linkmod_typeflags );
408+
409+
// should be no differences thus empty array, this being TRUE also means
410+
// that text was exact match in the updated array vs the original.
411+
$this->assertTrue( empty( $linkmod_differences ) );
412+
413+
414+
// repeat some of the above tests but this time with depth = 1 so that we catch classes intended for inside dropdowns.
415+
$depth = 1;
416+
$linkmod_classes_d = array();
417+
$icon_classes_d = array();
418+
$returned_array_d = $method_open->invokeArgs( $wp_bootstrap_navwalker, array( $linkmod_array, &$linkmod_classes_d, &$icon_classes_d, $depth ) );
419+
420+
$this->assertTrue( count( $this->valid_linkmod_classes ) === count( $linkmod_classes_d ), "Seems that the linkmod classes are not catptured properly when inside dropdowns... \nvalid: \n" . print_r( $this->valid_linkmod_classes, true ) . "\nreturned: \n" . print_r( $linkmod_classes, true ) );
421+
$linkmod_differences_d = array_diff( $this->valid_linkmod_classes, $linkmod_classes_d );
422+
$this->assertTrue( empty( $linkmod_differences_d ), 'There are differences between the matched classnames and the valid classnames.' );
423+
424+
}
425+
426+
/**
427+
* Test that the function catches all possible linkmod classes, any icon
428+
* classes and leaves the other classes as-is on the array.
429+
*
430+
* @depends test_seporate_linkmods_and_icons_from_classes_function_exists
431+
*
432+
* @depends test_seporate_linkmods_and_icons_from_classes_fontawesome
433+
* @depends test_seporate_linkmods_and_icons_from_classes_glyphicons
434+
* @depends test_seporate_linkmods_and_icons_from_classes_linkmods
435+
*
436+
* @access public
437+
* @return void
438+
*/
439+
function test_seporate_linkmods_and_icons_from_classes_fulltest() {
440+
441+
$wp_bootstrap_navwalker = $this->walker;
442+
// since we're working with private methods we need to use a reflector.
443+
$reflector = new ReflectionClass( 'WP_Bootstrap_Navwalker' );
444+
445+
// get a reflected method for the opener function and set to public.
446+
$method_open = $reflector->getMethod( 'seporate_linkmods_and_icons_from_classes' );
447+
$method_open->setAccessible( true );
448+
449+
$icons_array = array_merge( $this->some_fontawesome_classes, $this->some_glyphicons_classes );
450+
$linkmod_array = $this->valid_linkmod_classes;
451+
$linkmod_classes = array();
452+
$icon_classes = array();
453+
$chars = range( 'a', 'z' );
454+
$extra_classes = array();
455+
// make 1000 random valid classnames with legth of 8 chars (there should
456+
// be no naming collisions here with this random string gen method).
457+
for ( $i = 0; $i < 1000; $i++ ) {
458+
$string = '';
459+
$length = mt_rand( 4, 10 );
460+
for ( $j = 0; $j < $length; $j++ ) {
461+
$string .= $chars[ mt_rand( 0, count( $chars ) - 1 ) ];
462+
}
463+
$extra_classes[] = $string;
464+
}
465+
// merge the valid icon classes with valid linkmod classes and the extra classes then shuffle order.
466+
$classname_array = array_merge( $icons_array, $linkmod_array, $extra_classes );
467+
shuffle( $classname_array );
468+
469+
// need a depth of 1 to ensure that our linkmods classes for inside dropdowns are also captured.
470+
$depth = 1;
471+
$returned_array = $method_open->invokeArgs( $wp_bootstrap_navwalker, array( $classname_array, &$linkmod_classes, &$icon_classes, $depth ) );
472+
473+
// linkmod_classes NOT should be empty and returned_array should not.
474+
$this->assertTrue( ( ! empty( $linkmod_classes ) && ! empty( $returned_array ) ), 'Either the linkmod array of the returned non matching classes array is empty when they shoud not be.' );
475+
// starting arrays should no longer be empty.
476+
$this->assertNotTrue( empty( $icon_classes ), 'Did not catch any icons.' );
477+
$this->assertNotTrue( empty( $linkmod_classes ), 'Did not catch any linkmods.' );
478+
479+
// icons compair.
480+
$this->assertTrue( count( $icons_array ) === count( $icon_classes ), "Seems that icon classes are not catptured properly... valid: \n" . print_r( $icons_array, true ) . "returned: \n" . print_r( $icon_classes, true ) );
481+
$icon_differences = array_diff( $icons_array, $icon_classes );
482+
$this->assertTrue( empty( $icon_differences ), 'Seems that we did not catch all of the icon classes.' );
483+
// linkmod compair.
484+
$this->assertTrue( count( $linkmod_array ) === count( $linkmod_classes ), "Seems that linkmod classes are not catptured properly... valid: \n" . print_r( $linkmod_array, true ) . "returned: \n" . print_r( $linkmod_classes, true ) );
485+
$linkmod_differences = array_diff( $icons_array, $icon_classes );
486+
$this->assertTrue( empty( $linkmod_differences ), 'Seems that we did not catch all of the linkmod classes.' );
487+
// extra classes string matches checks.
488+
$returned_differences = array_diff( $returned_array, $extra_classes );
489+
$this->assertTrue( empty( $returned_differences ), 'The returned array minus the extra classes should be empty, likely some classes were missed or string malformation occured.' );
490+
491+
}
492+
209493
/**
210494
* Test get_linkmod_type Function exists.
211495
*
@@ -274,6 +558,13 @@ function test_linkmod_element_close_function_exists() {
274558

275559
}
276560

561+
/**
562+
* Tests for valid markup being used as the opener and closer sections for
563+
* some different linkmod types.
564+
*
565+
* @access public
566+
* @return void
567+
*/
277568
function test_linkmod_elements_open_and_close_successfully() {
278569

279570
$wp_bootstrap_navwalker = $this->walker;

0 commit comments

Comments
 (0)