SilverStripe functional testing for CMS route (settings) always returning 404 for POST request - silverstripe

I am working on a SilverStripe project. I am writing functional tests for my application. I am writing a function test for a path within the CMS. In my case the path is /admin/settings/SecondaryMenuPages. It is the path within the CMS and so the user needs to be logged in.
This is my test class.
class MegaMenuTest extends FunctionalTest
{
protected static $fixture_file = [
'app/tests/Cms/cms_admin_user_fixture.yml',
'app/tests/Cms/secondary_menu_fixture.yml',
];
function testCmsUserCanUnlinkSecondaryMenuItem()
{
$member = $this->objFromFixture(Member::class, 'admin_user');
$adminGroup = $this->objFromFixture(Group::class, 'admin_group');
$this->assignMemberToGroup($member, $adminGroup);
$this->assignPageToSecondaryMenu();
$this->logInAs($member);
$this->post("/admin/settings/SecondaryMenuPages", [
'ID' => 1,
], null, null, json_encode([ 'ID' => 1 ]));
}
private function assignMemberToGroup($member, $adminGroup)
{
$sqlInsert = SQLInsert::create('Group_Members');
$sqlInsert->addRows([
[ '"GroupID"' => $adminGroup->ID, '"MemberID"' => $member->ID ],
]);
$sqlInsert->execute();
}
private function assignPageToSecondaryMenu()
{
$page = $this->objFromFixture('Page', 'page_one');
$siteConfig = $this->objFromFixture(SiteConfig::class, 'siteconfig_one');
$sqlInsert = SQLInsert::create('SiteConfig_SecondaryMenuPages');
$sqlInsert->addRows([
[ '"PageID"' => $page->ID, '"SiteConfigID"' => $siteConfig->ID ],
]);
$sqlInsert->execute();
}
}
Literally, the page is under the settings within the CMS.
I seeded the admin user in the cms_admin_user_fixture.yml file as follows.
SilverStripe\Security\Member:
admin_user:
ID: 1
LastEdited: 2020-03-28 17:15:53
Created: 2019-01-08 17:04:53
FirstName: Default Admin
Email: admin
SilverStripe\Security\Group:
admin_group:
ID: 1
LastEdited: 2019-01-08 17:04:53
Created: 2019-01-08 17:04:53
Title: Administrators
Code: administrators
Locked: 0
I seeded the page and required data for request in the secondary_menu_fixture.yml file as follows.
SilverStripe\CMS\Model\SiteTree:
sitetree_page_one:
ID: 1
ClassName: Page
Title: Page Title 1
URLSegment: page-url-1
Page:
page_one:
ID: 1
Heading: Page Title 1
Title: Cision form content
URLSegment: page-url-1
SilverStripe\SiteConfig\SiteConfig:
siteconfig_one:
ID: 1
LastEdited: 2020-03-10 12:25:37
Created: 2019-01-08 17:04:53
Title: Siteconfig Title 1
Tagline: Siteconfig Tagline 1
CanViewType: Anyone
CanEditType: OnlyTheseUsers
CanCreateTopLevelType: OnlyTheseUsers
When I run my test, the post request to this path, "/admin/settings/SecondaryMenuPages" is always returning 404. What is wrong with my code and how can I fix it?

Related

Vue3: redundant route.push, unexpected route redirect after some navigation

Good day all !
I have an unwanted redirect occurring from a manual route.push, after some navigation steps, as if it had been cached in history stack.
Edit: the only alternative i have found, is to no longer need home dependency and copying it to [A], then, no need to pushing the route causing the issue (but i dont like this code duplication).
Said more exactly, [A] has two uses, first is a data listing, second one is to fetch and print data from elements of this list (home has already the code for fetching and printing), then i call [home] from [A] with suitable parameters to avoid code duplication.
The parameters are provided by a custom store (not pinia store)
Any clue ?
Note: i'm using vue3/router4 and only composition features are used
Code overview:
component Home
// do something...
// click methods to go to [B]
component A
setup()
{
// called by #click
search = (id)
{
store.query.value = id
route.push({name:'home'})
}
...
component B
// do something
Steps to reproduce:
- opening app with [home] comp
- go to [A],
- then search click (to prepare query for home api call)
- comming from [A] to home (by route.push...)
- button click to go to [B] from home
- [B] loads but the route change unexpectedly to home before [B] route end
my logs say the route is already redirected when we arrive in the route guard beforeEach()
here is a summary of the router code
const routes = [
{
path: '/', // is [home]
name: 'root',
component: Home,
meta: { title: false }
},
{
path: '/categories', // is [A]
name: 'categories',
component: () => import('./views/Categories.vue'),
meta: { title: 'route.categories' }
},
{
path: '/tool/:id', // is [B]
name: 'tool',
component: () => import('./views/Tool.vue'),
meta: { title: false }
},
{
path: '/:pathMatch(.*)*',
name: '404',
component: () => import('./components/404.vue'),
meta: { title: 'route.404' }
}
]
export const router = createRouter({
history: createWebHistory(process.env.BASE_URL),
base: process.env.BASE_URL,
routes,
navigationFallback:
{
rewrite: '/',
exclude: ['/images/*.{png,jpg,gif}', '/css/*']
}
})
Expected behavior :
I would like to void this not wished redirect
I tried to use route.replace instead of push, nothing changed,
commented many piece of code, and this is only when i comment the route.push from [A], that the redirect no longer occurs.
I suspect the route.push in [A] to be cached somewhere. I can't understand why it runs twice (at call, and after two route navigation steps...)

zendframework 3 - identity view helper error

I try to use identity view helper in my layout.phtml
$this->identity();
but get this error:
No AuthenticationServiceInterface instance provided in vendor\zendframework\zend-view\src\Helper\Identity.php on line 38
Did you actually setup Zend-Authentication and registered it within your services of the ServiceManager?
Because the factory for the IdentityHelper will set the AuthenticationService.
See: https://github.com/zendframework/zend-mvc-plugin-identity/blob/1.1.0/src/IdentityFactory.php#L27-L31
if ($container->has(AuthenticationService::class)) {
$plugin->setAuthenticationService($container->get(AuthenticationService::class));
} elseif ($container->has(AuthenticationServiceInterface::class)) {
$plugin->setAuthenticationService($container->get(AuthenticationServiceInterface::class));
}
Register the AuthenticationService within your Applications module.config.php:
'service_manager' => [
'factories' => [
\Zend\Authentication\AuthenticationService::class => \Application\Service\Factory\AuthenticationServiceFactory::class,
]
],
Create a factory for your AuthenticationService where you tell the service which adapter to use on how to check if the identity is valid. See https://docs.zendframework.com/zend-authentication/intro/#usage

Drupal 7 Services create node with images

I installed a new Drupal 7.56 distribution.
I want to create new nodes via java client.
However, After little googling I found the “Services” module for Drupal 7.
So I installed it, with the next module:
Services
Ctools (required for Services)
Libraries (required for Services)
OAuth 1.0
So, I installed these modules. From the Structure menu I created the Service.
Endpoint: API
Server: REST
I enabled the Session and OAuth authentication.
I created a new Content type.
name: TestContent (Machine name: testcontent)
Fields:
Title (M.n.: title)
Body (M.n.: body)
Pics (M.n: field_pics) (Type: Image) Number of values: 5
In this Service I enabled all resource (file, user, etc..)
I Disabled the OAuth, because I will set up it later.
Now, I opened my Postman client.
Logging in: admin/admin
{
"sessid": "QZTYSQu3-I9pacOpoSP--V_LkGcusy2grl12U_CyKrY",
"session_name": "SESS51ebf8732a20744576a234cf7af43040",
"token": "jkUDb6MsGMHt_mBlGbm02O-lyZq-2nRTqD1OslxtvAg",
"user": {
"uid": "6",
"name": "admin",
…
Now I have a token. Now I upload two picture.
http://test.dd:8083/api/file
Got these responses
{
"fid": "6",
"uri": "http://test.dd:8083/api/file/6"
}
{
"fid": “7”,
"uri": "http://test.dd:8083/api/file/7”
}
Ok, Now I’ll try to create a new TestContent, and connect these Images to the node.
Ok, The node is created. But the Images isn’t connected to the node. But I didn’t get error message.
Why? What’s wrong?
I tried:
[ {fid:6} , {fid:7}]
und: [ { fid: 6 }]
Please give me ideas. Thank you
I found the solution on the Drupal site.
Run these GIT difference!
diff --git a/resources/node_resource.inc b/resources/node_resource.inc
index 8f870b7..6669a1a 100644
--- a/resources/node_resource.inc
+++ b/resources/node_resource.inc
## -339,8 +339,9 ## function _node_resource_create($node) {
);
$stub_form = drupal_build_form($node_type . '_node_form', $stub_form_state);
$form_state['triggering_element'] = $stub_form['actions']['submit'];
+ $node = (object) array_merge((array) $stub_node, (array) $node);
- drupal_form_submit($node_type . '_node_form', $form_state, (object)$stub_node);
+ drupal_form_submit($node_type . '_node_form', $form_state, $node);
if ($errors = form_get_errors()) {
return services_error(implode(" ", $errors), 406, array('form_errors' => $errors));
## -423,6 +424,7 ## function _node_resource_update($nid, $node) {
$node_type = $node['type'];
node_object_prepare($old_node);
+ $old_node = (object) array_merge((array) $old_node, (array) $node);
// Setup form_state.
$form_state = array();
diff --git a/resources/user_resource.inc b/resources/user_resource.inc
index 304a293..c1f9b5a 100644
--- a/resources/user_resource.inc
+++ b/resources/user_resource.inc
## -471,6 +471,7 ## function _user_resource_update($uid, $account) {
return services_error(t('You are not allowed to change your username.'), 406);
}
+ $account_loaded = (object) array_merge((array) $account_loaded, (array) $account);
$form_state['values']['op'] = variable_get('services_user_save_button_resource_update', t('Save'));
$form_state['values']['#user_category'] = $category;
$form_state['values']['#account'] = $account_loaded;
See more: Here

Drupal 8 migrate plugin - explanation needed

I want to migrate data from custom table to book content type, however I have no idea how to achieve that.
This is my config\install\migrate.migration.book_content.yml file:
id: book_content
label: Book content
migration_group: example
source:
plugin: book_content
target: db_migration
destination:
plugin: entity:node
process:
type:
plugin: default_value
default_value: article
title: title
uid:
plugin: default_value
default_value: 1
sticky:
plugin: default_value
default_value: 0
status:
plugin: default_value
default_value: 1
'body/value': content
'body/summary': excerpt
'body/format':
plugin: default_value
default_value: full_html
# Use of the migration process plugin.
# That will use the id map of the specified migration
# to retrieve the corresponding id ('source' parameter) in the destination database.
field_description:
plugin: migration
source: body
src\Plugin\migrate\source\BookContent.php
<?php
/**
* #file
* Contains \Drupal\docapi_migrate\Plugin\migrate\source\BookContent.
*/
namespace Drupal\example_migrate\Plugin\migrate\source;
use Drupal\migrate\Plugin\migrate\source\SqlBase;
use Drupal\migrate\Row;
/**
* Drupal 8 node from database.
*
* #MigrateSource(
* id = "book_content"
* )
*/
class BookContent extends SqlBase {
/**
* {#inheritdoc}
*/
public function query() {
$query = $this->select('docapi_migrate', 'dm')
->fields('dm', array('url', 'depth', 'body', 'meta'));
return $query;
}
/**
* {#inheritdoc}
*/
public function fields() {
$fields = array(
'url' => $this->t('Content url'),
'depth' => $this->t('Depth of the content'),
'body' => $this->t('Full text of the content'),
'meta' => $this->t('yaml data'),
);
return $fields;
}
However when I do:
drush migrate-import book_content
I get:
The drush command 'migrate-import book_content' could not be found.
I don't understand how this functions in BookContent.php work.
There should be the tags_field with is taxonomy reference, but I have no idea, where should I process the meta, so for example I get a list of Tags.
And when I have tags, how should I combine it, so the migration works.
I was searching via google for information, but nothing really make sense. How can I understand this?
You namespace is set to example_migrate. It needs to be set to the name of your module, so unless the module is called "example_migrate" it cannot find the class.
Change the name space to match YOUR_MODULE_NAME.
namespace Drupal\YOUR_MODULE_NAME\Plugin\migrate\source;
More about namespaces can be found when Googling for PSR0.

Metalsmith-permalinks failing to add path to metadata

I'm using metalsmith-permalinks with the following settings:
.use(permalinks({
pattern: ':title',
linksets: [{
match: { collection: 'blogposts' },
pattern: 'blog/:title'
}, {
match: { collection: 'portfolioposts' },
pattern: 'portfolio/:title'
}]
}))
However, logging files in the relevant collections shows that path isn't being added to their metadata:
layout: 'subpage.hbs',
title: 'Blog Post',
date: 2016-08-16T00:00:00.000Z,
excerpt: 'This post has an excerpt',
tags: [ 'test', 'test-tag', 'a-third-tag' ],
contents: <>,
mode: '0644',
stats: {},
collection: [ 'blogposts' ]
Permalinks is being called before layouts, meaning it can't be the same issue found here, and – as seen in the log – other plugins are adding metadata successfully.
My full build order is:
.use(collections({}))
.use(tags({}))
.use(permalinks({}))
.use(dateFormatter({}))
.use(markdown({}))
.use(helpers({}))
.use(layouts({}))
.use(rename({}))
And I'm running Metalsmith directly in Gulp. Any help would be greatly appreciated.

Resources