Skip to content

Commit b19b923

Browse files
authored
[5.2] SEF: Add option to don't set Itemid to homepage by default (joomla#42989)
* SEF: Add option to don't set Itemid to homepage by default * codestyle * Adding option to tags router * Update Router.php * Update MenuRules.php * Switch to strictrouting * Fixing router behavior for broken URLs * Fixing bug in system tests * Removing unnecessary test for user reminder * Fixing profile edit test to go back to form
1 parent 50726be commit b19b923

File tree

8 files changed

+80
-67
lines changed

8 files changed

+80
-67
lines changed

components/com_contact/src/Service/Router.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,14 @@ public function getContactId($segment, $query)
249249
$dbquery = $this->db->getQuery(true);
250250
$dbquery->select($this->db->quoteName('id'))
251251
->from($this->db->quoteName('#__contact_details'))
252-
->where(
253-
[
254-
$this->db->quoteName('alias') . ' = :alias',
255-
$this->db->quoteName('catid') . ' = :catid',
256-
]
257-
)
258-
->bind(':alias', $segment)
259-
->bind(':catid', $query['id'], ParameterType::INTEGER);
252+
->where($this->db->quoteName('alias') . ' = :alias')
253+
->bind(':alias', $segment);
254+
255+
if (isset($query['id']) && $query['id']) {
256+
$dbquery->where($this->db->quoteName('catid') . ' = :catid')
257+
->bind(':catid', $query['id'], ParameterType::INTEGER);
258+
}
259+
260260
$this->db->setQuery($dbquery);
261261

262262
return (int) $this->db->loadResult();

components/com_content/src/Service/Router.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,14 @@ public function getArticleId($segment, $query)
250250
$dbquery = $this->db->getQuery(true);
251251
$dbquery->select($this->db->quoteName('id'))
252252
->from($this->db->quoteName('#__content'))
253-
->where(
254-
[
255-
$this->db->quoteName('alias') . ' = :alias',
256-
$this->db->quoteName('catid') . ' = :catid',
257-
]
258-
)
259-
->bind(':alias', $segment)
260-
->bind(':catid', $query['id'], ParameterType::INTEGER);
253+
->where($this->db->quoteName('alias') . ' = :segment')
254+
->bind(':segment', $segment);
255+
256+
if (isset($query['id']) && $query['id']) {
257+
$dbquery->where($this->db->quoteName('catid') . ' = :id')
258+
->bind(':id', $query['id'], ParameterType::INTEGER);
259+
}
260+
261261
$this->db->setQuery($dbquery);
262262

263263
return (int) $this->db->loadResult();

components/com_newsfeeds/src/Service/Router.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,14 +230,14 @@ public function getNewsfeedId($segment, $query)
230230
$dbquery = $this->db->getQuery(true);
231231
$dbquery->select($this->db->quoteName('id'))
232232
->from($this->db->quoteName('#__newsfeeds'))
233-
->where(
234-
[
235-
$this->db->quoteName('alias') . ' = :segment',
236-
$this->db->quoteName('catid') . ' = :id',
237-
]
238-
)
239-
->bind(':segment', $segment)
240-
->bind(':id', $query['id'], ParameterType::INTEGER);
233+
->where($this->db->quoteName('alias') . ' = :segment')
234+
->bind(':segment', $segment);
235+
236+
if (isset($query['id']) && $query['id']) {
237+
$dbquery->where($this->db->quoteName('catid') . ' = :id')
238+
->bind(':id', $query['id'], ParameterType::INTEGER);
239+
}
240+
241241
$this->db->setQuery($dbquery);
242242

243243
return (int) $this->db->loadResult();

components/com_tags/src/Service/Router.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
use Joomla\CMS\Component\Router\RouterBase;
1717
use Joomla\CMS\Language\Multilanguage;
1818
use Joomla\CMS\Menu\AbstractMenu;
19+
use Joomla\CMS\Plugin\PluginHelper;
1920
use Joomla\Database\DatabaseInterface;
21+
use Joomla\Registry\Registry;
2022
use Joomla\Utilities\ArrayHelper;
2123

2224
// phpcs:disable PSR1.Files.SideEffects
@@ -47,6 +49,16 @@ class Router extends RouterBase
4749
*/
4850
protected $lookup = [];
4951

52+
/**
53+
* System - SEF Plugin parameters
54+
*
55+
* @var Registry
56+
* @since __DEPLOY_VERSION__
57+
* @deprecated __DEPLOY_VERSION__ will be removed in 6.0
58+
* without replacement
59+
*/
60+
private $sefparams;
61+
5062
/**
5163
* Tags Component router constructor
5264
*
@@ -63,6 +75,9 @@ public function __construct(SiteApplication $app, AbstractMenu $menu, ?CategoryF
6375

6476
parent::__construct($app, $menu);
6577

78+
$sefPlugin = PluginHelper::getPlugin('system', 'sef');
79+
$this->sefparams = new Registry($sefPlugin->params);
80+
6681
$this->buildLookup();
6782
}
6883

@@ -141,12 +156,15 @@ public function preprocess($query)
141156
}
142157
}
143158

144-
// If not found, return language specific home link
145-
if (!isset($query['Itemid'])) {
146-
$default = $this->menu->getDefault($lang);
159+
// TODO: Remove this whole block in 6.0 as it is a bug
160+
if (!$this->sefparams->get('strictrouting', 0)) {
161+
// If not found, return language specific home link
162+
if (!isset($query['Itemid'])) {
163+
$default = $this->menu->getDefault($lang);
147164

148-
if (!empty($default->id)) {
149-
$query['Itemid'] = $default->id;
165+
if (!empty($default->id)) {
166+
$query['Itemid'] = $default->id;
167+
}
150168
}
151169
}
152170

libraries/src/Component/Router/Rules/MenuRules.php

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Joomla\CMS\Component\ComponentHelper;
1313
use Joomla\CMS\Component\Router\RouterView;
1414
use Joomla\CMS\Language\Multilanguage;
15+
use Joomla\CMS\Plugin\PluginHelper;
16+
use Joomla\Registry\Registry;
1517

1618
// phpcs:disable PSR1.Files.SideEffects
1719
\defined('_JEXEC') or die;
@@ -40,6 +42,16 @@ class MenuRules implements RulesInterface
4042
*/
4143
protected $lookup = [];
4244

45+
/**
46+
* System - SEF Plugin parameters
47+
*
48+
* @var Registry
49+
* @since __DEPLOY_VERSION__
50+
* @deprecated __DEPLOY_VERSION__ will be removed in 6.0
51+
* without replacement
52+
*/
53+
private $sefparams;
54+
4355
/**
4456
* Class constructor.
4557
*
@@ -49,7 +61,9 @@ class MenuRules implements RulesInterface
4961
*/
5062
public function __construct(RouterView $router)
5163
{
52-
$this->router = $router;
64+
$this->router = $router;
65+
$sefPlugin = PluginHelper::getPlugin('system', 'sef');
66+
$this->sefparams = new Registry($sefPlugin->params);
5367

5468
$this->buildLookup();
5569
}
@@ -152,21 +166,24 @@ public function preprocess(&$query)
152166
}
153167
}
154168

155-
// Check if the active menuitem matches the requested language
156-
if (
157-
$active && $active->component === 'com_' . $this->router->getName()
158-
&& ($language === '*' || \in_array($active->language, ['*', $language]) || !Multilanguage::isEnabled())
159-
) {
160-
$query['Itemid'] = $active->id;
169+
// TODO: Remove this whole block in 6.0 as it is a bug
170+
if (!$this->sefparams->get('strictrouting', 0)) {
171+
// Check if the active menuitem matches the requested language
172+
if (
173+
$active && $active->component === 'com_' . $this->router->getName()
174+
&& ($language === '*' || \in_array($active->language, ['*', $language]) || !Multilanguage::isEnabled())
175+
) {
176+
$query['Itemid'] = $active->id;
161177

162-
return;
163-
}
178+
return;
179+
}
164180

165-
// If not found, return language specific home link
166-
$default = $this->router->menu->getDefault($language);
181+
// If not found, return language specific home link
182+
$default = $this->router->menu->getDefault($language);
167183

168-
if (!empty($default->id)) {
169-
$query['Itemid'] = $default->id;
184+
if (!empty($default->id)) {
185+
$query['Itemid'] = $default->id;
186+
}
170187
}
171188
}
172189

tests/System/integration/site/components/com_contact/Contact.cy.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ describe('Test in frontend that the contact details view', () => {
22
it('can display a form', () => {
33
cy.db_getUserId().then((id) => cy.db_createContact({ name: 'contact 1', user_id: id }))
44
.then((contact) => {
5-
cy.visit(`/index.php?option=com_contact&view=contact&id='${contact.id}'`);
5+
cy.visit(`/index.php?option=com_contact&view=contact&id=${contact.id}`);
66

77
cy.contains('Contact Form');
88
cy.get('.m-0').should('exist');
@@ -17,7 +17,7 @@ describe('Test in frontend that the contact details view', () => {
1717
.then(() => cy.db_getUserId())
1818
.then((userId) => cy.db_createContact({ name: 'automated test contact 1', user_id: userId }))
1919
.then((contact) => {
20-
cy.visit(`/index.php?option=com_contact&view=contact&id='${contact.id}'`);
20+
cy.visit(`/index.php?option=com_contact&view=contact&id=${contact.id}`);
2121

2222
cy.contains('automated test_field group').should('exist');
2323
cy.contains('test field').should('exist');

tests/System/integration/site/components/com_users/Profile_Edit.cy.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ describe('Test in frontend that the users profile view edit layout', () => {
5050
cy.get('.controls > .btn-primary').should('be.visible').click({ force: true });
5151

5252
cy.get('#system-message-container').contains('Profile saved.');
53+
cy.get('.profile .btn-primary').should('be.visible').click({ force: true });
5354
cy.get('#jform_name').should('have.value', 'automated test user edited');
5455
cy.get('#jform_email1').should('have.value', '[email protected]');
5556
});

tests/System/integration/site/components/com_users/Remind.cy.js

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,6 @@
11
describe('Test in frontend that the users remind view', () => {
22
beforeEach(() => cy.task('clearEmails'));
33

4-
it('can send a reminder email for a test user in a menu item', () => {
5-
cy.db_createUser({ name: 'test user', email: '[email protected]' })
6-
.then(() => cy.db_createMenuItem({
7-
title: 'Automated test reminder', alias: 'test-reminder', path: 'test-reminder', link: 'index.php?option=com_users&view=remind',
8-
}))
9-
.then(() => {
10-
cy.visit('/');
11-
cy.get('a:contains(Automated test reminder)').click();
12-
cy.get('#jform_email').type('[email protected]');
13-
cy.get('.controls > .btn').click();
14-
15-
cy.task('getMails').then((mails) => {
16-
cy.get('#system-message-container').should('contain.text', 'If the email address you entered is registered on this site you will shortly receive an email with a reminder.');
17-
18-
expect(mails.length).to.equal(1);
19-
cy.wrap(mails[0].body).should('have.string', 'A username reminder has been requested');
20-
cy.wrap(mails[0].body).should('have.string', '/test-reminder');
21-
cy.wrap(mails[0].sender).should('equal', Cypress.env('email'));
22-
cy.wrap(mails[0].receivers).should('have.property', '[email protected]');
23-
});
24-
});
25-
});
26-
274
it('can send a reminder email for a test user without a menu item', () => {
285
cy.db_createUser({ name: 'test user', email: '[email protected]' })
296
.then(() => {

0 commit comments

Comments
 (0)