FOSSology  4.7.1
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 test_common_menu 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  if (!is_array($MenuList)) {
81  $MenuList = array();
82  }
83 
84  $path = "TestMenu::Test1::Test2";
85  $lastOrder = 0;
86  $uri = "TestURI";
87  $title = "TestTitle";
88  $target = "TestTarget";
89  $html = "TestHTML";
90  $countMenuListBefore = count($MenuList);
91 
92  menu_insert($path, $lastOrder, $uri, $title, $target, $html);
93 
94  // Assert that menu_insert correctly inserts the menu item
95  $this->assertEquals($path, $MenuList[$countMenuListBefore]->SubMenu[0]->SubMenu[0]->FullName);
96 
97  $depth = 2;
98  $result = menu_find("Test1", $depth);
99 
100  // Assert that menu_to_1html() generates correct output
101  $result = menu_to_1html($MenuList);
102  $pattern = "/TestMenu/";
103  if (intval(explode('.', PHPUnitVersion::id())[0]) >= 9) {
104  $this->assertMatchesRegularExpression($pattern, $result);
105  } else {
106  $this->assertRegExp($pattern, $result);
107  }
108 
109  $parm = "";
110  $result = menu_to_1list($MenuList, $parm, "", "");
111  // Assert that menu_to_1list() generates correct output
112  if (intval(explode('.', PHPUnitVersion::id())[0]) >= 9) {
113  $this->assertMatchesRegularExpression($pattern, $result);
114  } else {
115  $this->assertRegExp($pattern, $result);
116  }
117  }
118 
122  protected function tearDown(): void
123  {
124  // Additional teardown code, if needed
125  }
126 }
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:62
$MenuList
Global menu list array.
Definition: common-menu.php:50
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.