FOSSology  4.6.0
Open Source License Compliance by Open Source Software
test_common_menu.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2011 Hewlett-Packard Development Company, L.P.
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
12 namespace Fossology\Tests;
13 
14 use PHPUnit\Framework\TestCase;
15 use PHPUnit\Runner\Version as PHPUnitVersion;
16 
17 require_once dirname(__FILE__) . '/../common-menu.php';
18 require_once dirname(__FILE__) . '/../common-parm.php';
19 
24 class CommonMenuTest extends TestCase
25 {
29  public function testMenuPage()
30  {
31  $page = 10;
32  $totalPage = 15;
33  $uri = "http://fossology.org/repo/";
34  $expected = "<a class='page-link' href='http://fossology.org/repo/&page=9'>Prev</a>";
35  $result = MenuPage($page, $totalPage, $uri);
36  // Assert that the result contains the expected output
37  $this->assertStringContainsString("<a class='page-link' href='#'>11</a>", $result);
38  $this->assertStringContainsString($expected, $result);
39  }
40 
44  public function testMenuEndlessPage()
45  {
46  $page = 10;
47  $uri = "http://fossology.org/repo/";
48  $expected = "<a class='page-link' href='http://fossology.org/repo/&page=9'>Prev</a>";
49  $result = MenuEndlessPage($page, 1, $uri);
50  // Assert that the result contains the expected output
51  $this->assertStringContainsString("<a class='page-link' href='#'>11</a>", $result);
52  $this->assertStringContainsString($expected, $result);
53  }
54 
58  public function testMenuCmp()
59  {
60  $menua = new \menu();
61  $menub = new \menu();
62  $menua->Name = 'menua';
63  $menub->Name = 'menua';
64 
65  // Assert that menu_cmp returns 0 for identical menus
66  $this->assertEquals(0, menu_cmp($menua, $menub));
67 
68  $menua->Order = 1;
69  $menub->Order = 2;
70  // Assert that menu_cmp returns 1 when menua has a higher order than menub
71  $this->assertEquals(1, menu_cmp($menua, $menub));
72  }
73 
77  public function testMenuFunctions()
78  {
79  global $MenuList;
80 
81  $path = "TestMenu::Test1::Test2";
82  $lastOrder = 0;
83  $uri = "TestURI";
84  $title = "TestTitle";
85  $target = "TestTarget";
86  $html = "TestHTML";
87  $countMenuListBefore = count($MenuList);
88 
89  menu_insert($path, $lastOrder, $uri, $title, $target, $html);
90 
91  // Assert that menu_insert correctly inserts the menu item
92  $this->assertEquals($path, $MenuList[$countMenuListBefore]->SubMenu[0]->SubMenu[0]->FullName);
93 
94  $depth = 2;
95  $result = menu_find("Test1", $depth);
96 
97  // Assert that menu_to_1html() generates correct output
98  $result = menu_to_1html($MenuList);
99  $pattern = "/TestMenu/";
100  if (intval(explode('.', PHPUnitVersion::id())[0]) >= 9) {
101  $this->assertMatchesRegularExpression($pattern, $result);
102  } else {
103  $this->assertRegExp($pattern, $result);
104  }
105 
106  $parm = "";
107  $result = menu_to_1list($MenuList, $parm, "", "");
108  // Assert that menu_to_1list() generates correct output
109  if (intval(explode('.', PHPUnitVersion::id())[0]) >= 9) {
110  $this->assertMatchesRegularExpression($pattern, $result);
111  } else {
112  $this->assertRegExp($pattern, $result);
113  }
114  }
115 
119  protected function tearDown(): void
120  {
121  // Additional teardown code, if needed
122  }
123 }
Unit test for common-menu.php.
MenuEndlessPage($Page, $Next=1, $Uri='')
Create a "First Prev 1 2 ... Next" page links for paged output.
menu_insert($Path, $LastOrder=0, $URI=NULL, $Title=NULL, $Target=NULL, $HTML=NULL)
Given a Path, order level for the last item, and optional plugin name, insert the menu item.
menu_to_1list($Menu, &$Parm, $Pre="", $Post="", $ShowAll=1, $upload_id="")
Take a menu and render it as one HTML line with items in a "[name]" list.
MenuPage($Page, $TotalPage, $Uri='')
Create a "First Prev 1 2 ... Next Last" page links for paged output.
Definition: common-menu.php:60
$MenuList
Global menu list array.
Definition: common-menu.php:48
menu_cmp($a, $b)
Compare two menu items for sorting.
menu_find($Name, &$MaxDepth, $Menu=NULL)
Given a top-level menu name, find the list of sub-menus below it and max depth of menu.
menu_to_1html($Menu, $ShowRefresh=1, $ShowTraceback=0, $ShowAll=1)
Take a menu and render it as one HTML line.