/** * test for making a category search in the admin section of the website * NOTE: By adding a category in the database, this test will no longer work * unless $expectedResult is changed based on AdminSearch::makeCategorySearch's new output */ public function testMakeCategorySearch() { // Arrange $adminSearch = new AdminSearch(); $connection = open_database_connection(); $expectedResult = '<h3>Search results for Drinks</h3><form id="categoryResult3" action= "/adminEditCategory" method="get"> <p><span class="search" onclick="document.getElementById(\'categoryResult3\').submit();">Drinks</span> - Quench your thirst with our selection of freshly made drinks or warm yourself up with a selection of hot drinks that we offer!</p><input type="hidden" name="categoryID" value="3" /><input type="hidden" name="categoryName" value="Drinks" /><input type="hidden" name="categoryImageURL" value="orange_juice.png" /><input type="hidden" name="categorySummary" value="Quench your thirst with our selection of freshly made drinks or warm yourself up with a selection of hot drinks that we offer!" /><input type="hidden" name="categoryImageURL1" value="" /><input type="hidden" name="categoryImageURL2" value="orange_juice.png" /></form>'; // Assert $result = $adminSearch->makeCategorySearch($connection, "Drinks"); close_database_connection($connection); // Act $this->assertEquals($result, $expectedResult); }
/** * Search action for the admin pages. This function handles all of the possible * types of searches that the user might search for. * * Action for route: /adminSearch * * @param Request $request * @param Application $app * @return \Symfony\Component\HttpFoundation\RedirectResponse */ public function searchAction(Request $request, Application $app) { // test if 'username' stored in session ... $username = getAuthenticatedUserName($app); // check we are authenticated -------- $isAuthenticated = null != $username; if (!$isAuthenticated) { // not authenticated, so redirect to LOGIN page return $app->redirect('/login'); } $connection = open_database_connection(); $searchType = $_GET['search_type']; if ($searchType == "new") { $searchType = "product"; } $searchObject = new AdminSearch(); $searchResults = ""; if ($searchType == "category") { if (!empty($_GET['keywords'])) { if (isset($_GET['name'])) { if ($_GET['name'] == "ascending") { $searchResults = $searchObject->sortCategoryBySearch($connection, "ASC", "CategoryName", $_GET['keywords']); } else { if ($_GET['name'] == "descending") { $searchResults = $searchObject->sortCategoryBySearch($connection, "DESC", "CategoryName", $_GET['keywords']); } } } else { $searchResults = $searchObject->makeCategorySearch($connection, $_GET['keywords']); } } else { if (isset($_GET['name'])) { if ($_GET['name'] == "ascending") { $searchResults = $searchObject->sortCategory($connection, "CategoryName", "ASC"); } else { $searchResults = $searchObject->sortCategory($connection, "CategoryName", "DESC"); } } else { if (isset($_GET['summary'])) { if ($_GET['summary'] == "ascending") { $searchResults = $searchObject->sortCategory($connection, "CategorySummary", "ASC"); } else { $searchResults = $searchObject->sortCategory($connection, "CategorySummary", "DESC"); } } } } } else { if ($searchType == "product-category") { if (isset($_GET['search'])) { if (isset($_GET['name'])) { if ($_GET['name'] == "ascending") { $searchResults = $searchObject->sortCategoryBySearch($connection, "ASC", "CategoryName", $_GET['keywords']); } else { if ($_GET['name'] == "descending") { $searchResults = $searchObject->sortCategoryBySearch($connection, "DESC", "CategoryName", $_GET['keywords']); } } } else { $searchResults = $searchObject->makeCategorySearch($connection, $_GET['keywords']); } } else { if (isset($_GET['name'])) { if ($_GET['name'] == "ascending") { $searchResults = $searchObject->sortCategory($connection, "CategoryName", "ASC"); } else { $searchResults = $searchObject->sortCategory($connection, "CategoryName", "DESC"); } } else { if ($_GET['summary'] == "ascending") { $searchResults = $searchObject->sortCategory($connection, "CategorySummary", "ASC"); } else { $searchResults = $searchObject->sortCategory($connection, "CategorySummary", "DESC"); } } } } else { if ($searchType == "product") { if (isset($_GET['keywords'])) { if (isset($_GET['name'])) { if ($_GET['name'] == "ascending") { $searchResults = $searchObject->sortProductBySearch($connection, "ASC", "ProductName", $_GET['keywords']); } else { if ($_GET['name'] == "descending") { $searchResults = $searchObject->sortProductBySearch($connection, "DESC", "ProductName", $_GET['keywords']); } } } else { if (isset($_GET['calories'])) { if ($_GET['calories'] == "ascending") { $searchResults = $searchObject->sortProductBySearch($connection, "ASC", "ProductCalories", $_GET['keywords']); } else { if ($_GET['calories'] == "descending") { $searchResults = $searchObject->sortProductBySearch($connection, "DESC", "ProductCalories", $_GET['keywords']); } } } else { if (isset($_GET['price'])) { if ($_GET['price'] == "ascending") { $searchResults = $searchObject->sortProductBySearch($connection, "ASC", "ProductPrice", $_GET['keywords']); } else { if ($_GET['price'] == "descending") { $searchResults = $searchObject->sortProductBySearch($connection, "DESC", "ProductPrice", $_GET['keywords']); } } } else { if (isset($_GET['description'])) { if ($_GET['description'] == "ascending") { $searchResults = $searchObject->sortProductBySearch($connection, "ASC", "ProductDescription", $_GET['keywords']); } else { if ($_GET['description'] == "descending") { $searchResults = $searchObject->sortProductBySearch($connection, "DESC", "ProductDescription", $_GET['keywords']); } } } else { $searchResults = $searchObject->makeProductSearch($connection, $_GET['keywords']); } } } } } else { if (isset($_GET['name'])) { if ($_GET['name'] == "ascending") { $searchResults = $searchObject->sortProduct($connection, "ProductName", "ASC"); } else { $searchResults = $searchObject->sortProduct($connection, "ProductName", "DESC"); } } else { if (isset($_GET['calories'])) { if ($_GET['calories'] == "ascending") { $searchResults = $searchObject->sortProduct($connection, "ProductCalories", "ASC"); } else { $searchResults = $searchObject->sortProduct($connection, "ProductCalories", "DESC"); } } else { if (isset($_GET['description'])) { if ($_GET['description'] == "ascending") { $searchResults = $searchObject->sortProduct($connection, "ProductDescription", "ASC"); } else { $searchResults = $searchObject->sortProduct($connection, "ProductDescription", "DESC"); } } else { if (isset($_GET['price'])) { if ($_GET['price'] == "ascending") { $searchResults = $searchObject->sortProduct($connection, "ProductPrice", "ASC"); } else { $searchResults = $searchObject->sortProduct($connection, "ProductPrice", "DESC"); } } } } } } } } } close_database_connection($connection); // store username into args array $argsArray = array('title' => $_GET['keywords'] . " search results", 'username' => $username, 'searchType' => $searchType, 'searchResults' => $searchResults, 'search' => $_GET['keywords']); // render (draw) template // ------------ $templateName = 'admin/adminSearch'; return $app['twig']->render($templateName . '.html.twig', $argsArray); }