Call from cron_controller to another controller - codeigniter-3

This Cron_controller have access to view.
So now when I paste here, example function controller:
update_sitemap
now in routes I can set:
$route['cron/update-sitemap'] = 'cron_controller/update_sitemap';
and now I can set cron by running:
https://mywebsitecron.com/cron/update-sitemap
this working correct for login admin and logout admin.
ISSUE:
I've Helpdesk_controller - and this controller "have" only access to admin.
In Helpdesk_controller I've function:
read_emails
and now when I config in the same way:
$route['cron/read_emails'] = 'helpdesk_controller/read_emails';
I can run controller from link by paste in browser:
https://mywebsitecron.com/cron/read_emails
IMPORTANT: But I can run this link only when I login as admin.
So I cannot setup with this automatic run cron, because is restrict and redirect to login page users without admin login level.
and now I think how to resolve?
I think about open Cron_controller and write function;
/**
* Fetch Tickets
*/
public function fetch_tickets()
{
$this->helpdesk_controller->read_emails();
}
and then this one function will be call to another function to another controller.
but when I run fetch_tickets I get:
`Severity: Notice
Message: Undefined property: Cron_controller::$helpdesk_controller
Filename: controllers/Cron_controller.php
Line Number: 28`

Related

How to change action and call action/method from another controller?

I'm using Laminas framework (laminas/mvc v3.1) / Zend3. In certain cases I need to call a different action than prescribed for a given route, i.e. display a default page to users who are not authorised to view restricted content.
To do this, I've tweaked the onDispatch method in Module.php as follows:
public function onDispatch(MvcEvent $event) {
// check if content is restricted to the current user
if (content-is-restricted) {
$event->getRouteMatch()->setParam("controller", "{namespace}\Controller\{anotherController}");
$event->getRouteMatch()->setParam("action", "{defaultAction}");
$event->getRouteMatch()->setMatchedRouteName("{defaultRoute}");
}
else {
proceed-as-normal-to-the-action-prescribed-by-route
}
}
Unfortunately, the snippet above renders the following error:
A 404 error occurred
Page not found.
The requested controller was unable to dispatch the request.
Controller:
{namespace}\Controller\{anotherController}
No Exception available
This works though if the action is within the same controller but not if it's in a different one. What am I missing?
Note, I don't want to redirect the user and change the URL - just substitute what is showed to them

How to run one Cucumber (With Protractor) scenario over multiples files?

I have a Cucumber feature like this one:
#MainSuite
Scenario: Verify that user can login
Given I can see the login form
Then I set a username
And I set a password
And I click in Login button
Then I see the "wrong-password" message
I need to check that the user can login in 5 different pages.
I need to run that feature in 5 different places.
It's like, I need to run that feature in /login.html, /old_login.html, /after_restore_password.html and more (it's just an example).
Do you know how to do it?
Currently, I have only one file hardcoded. Obviously I need to change it.
this.Given(/^I can see the login form$/, function(done) {
this.goTo('login.html');
browser.wait(EC.visibilityOf(this.loginFormContainer));
done();
});
Create an object containing the different login pages that you can go to.
Include an if statement in your step def if there is any extra code that needs to be executed.
this.Given(/^I can see the "(.*)" login form$/, function(loginPage, done) {
var logins = {
login : "login.html",
afterpasswordreset: "after-password-reset.html"
}
loginPage = loginPage.toLowerCase().replace(" ", "");
this.goTo(logins[loginPage]);
browser.wait(EC.visibilityOf(this.loginFormContainer));
done();
});
Make either separate Scenarios or feature file for each variation of login, or simply create a Scenario Outline for them
EDIT
Here is how I would implement the Scenario Outline:
#MainSuite
Scenario Outline: Verify that user can login
Given I can see the "<loginType>" login form
And I set a username
And I set a password
When I click in Login button
Then I see the "wrong-password" message
Examples:
| loginType |
| Login |
| After Password Reset |

yii2 redirect from custom module to base controller

I use dektrium user registration form.
registration link generates domain.com/user/register link
but it's not base user model, this module is located inside vendor/dektrium folder.
Now in base controllers folder I have UsersController with view action.
and after finishing registration I want to start view action of UsersController to view users page.
This is registration module code
public function actionRegister()
{
if (!$this->module->enableRegistration) {
throw new NotFoundHttpException;
}
$model = $this->module->manager->createRegistrationForm();
if ($model->load(\Yii::$app->request->post()) && $model->register()) {
return $this->redirect(array('users/'.$model->username));
}
return $this->render('register', [
'model' => $model
]);
}
As you can see I've put there this code
return $this->redirect(array('users/'.$model->username));
Which is supposed to take user to it's own page at domain.com/users/username.
But unfortunatelly url is forming in the following way
domain.com/user/users/username
How can I fix this problem and direct user to domain.com/users/username page ?
Add an extra / in front of the users redirect. it should be
return $this->redirect(array('/users/'.$model->username));
Or you should actually create the url the proper way, that would be the best way to do this, but I do not know the way you have your rules set up so I cannot help you there. I am just guessing here but it should be:
return $this->redirect(array('users/view, 'username' => $model->username));
In this way you are using your url manager, not just hardcoding the url. In the future if you decide to change the link it will be much easier (replace just the url line in your config) and not go in files to change it.

Call to undefined function bp_core_get_user_email() - Buddypress issue

I am having a issue where I am trying to retrieve the email of the user who is logged in using Buddypress. Here is my code:
global $bp;
echo bp_core_get_user_email($bp->loggedin_user->id);
Here is the error message that pops up when I open the php page:
"Fatal error: Call to undefined function bp_core_get_user_email() in /home/user/public_html/useremail.php on line 4"
Have you loaded WordPress & BuddyPress in your file useremail.php?
I see that it's the same level as wp-config.php. To make it know anything about WP/BP functions you need to do at least this:
include ('./wp-load.php');
Otherwise in your situation that php file will through errors everytime you will use non-standard php functions.
But the true way is to use WP - create:
1) a plugin that will handle all requests to a specific url
OR
2) create a page in WP dahsboard with a specific page template, and in its template file you can write whatever code you need or want.
Other option to get the email:
$user_active = wp_get_current_user();
$user_mail = $user_active->user_email;

Unit Testing (PHPUnit): how to login?

I'm writing tests for my current project, made with Zend Framework.
Everything's fine, but I have a problem testing the logged users actions/controllers: I need to be logged in to be able to perform the action/controller.
How can I be logged in PHPUnit?
As you are saying you want to test actions/controllers, I suppose you are not writting unit-tests, but functional/integration tests -- ie, working with Zend_Test and testing via the MVC.
Here is a test-function I used in a project, where I'm testing if logging in is OK :
public function testLoggingInShouldBeOk()
{
$this->dispatch('/login/login');
$csrf = $this->_getLoginFormCSRF();
$this->resetResponse();
$this->request->setPost(array(
'login' => 'LOGIN',
'password' => 'PASSWORD',
'csrfLogin' => $csrf,
'ok' => 'Login',
));
$this->request->setMethod('POST');
$this->dispatch('/login/login');
$this->assertRedirectTo('/');
$this->assertTrue(Zend_Auth::getInstance()->hasIdentity());
}
Simply : I'm loading the login form, extracting the CSRF token, populating the form, and posting it.
Then, I can test if I'm connected.
With that, you can probably extract the logging-in part, to call it before each one of your tests that require a valid user to be logged-in.
There is another way. On my User entity I have a login() method that puts the user's id into the session and a static variable. What I just do in the test setUp() is call $user->login() and it works. In testing environment sessions are not used (setting Zend_Session::$isUnitTested = true has this effect) and tests rely on the static variable. Just remember to clear the static variable (logout() the user) on tearDown().
I think this article could help you:
http://perevodik.net/en/posts/7/
It describes how to create a fake identity you can use to set the environment to a state equivalent to a user being logged in.
In much the same way Pascal is using this function:
$this->_getLoginFormCSRF();
I have created a generic function that returns the value by loading the form using the form element manager:
public function _getCSRFHashValueFromForm($formAlias, $csrfName) {
$form = $this->servicemanager->get('FormElementManager')->get($formAlias);
return $form->get($csrfName)->getValue(); }
This of course assumes that the CSRF is bound to the form and not within any fieldset etc.