Homepage
Main navigation
Main content
Additional information
October Tricks
Login / Sign up
Quality Guidelines
About
GitHub
Propose new content
October CMS resources and help articles
Simple and to the point. Optimized by the community.
×
Login / Sign up
Quality Guidelines
About
GitHub
Propose new content
Edit trick
Changes will be published after manual review
Title
Give your trick a describing title. Do
not
start with «How to...».
Your trick
Keep it short and concise! Markdown is supported.
If you ever need to create a new user in your plugin unit tests, you first need to ensure early initialization of the Rainlab.User in your Plugin.php file, as below: ```php class Plugin extends PluginBase { // make sure the rainlab.user plugin is loaded already public $require = ['RainLab.User']; // ... continue with other initialization ``` Then, in your test class you can create new user: ```php // create a user $this->user = User::create([ 'name' => 'Some User', 'email' => 'test@test.com', 'password' => 'changeme!!!', 'password_confirmation' => 'changeme!!!' ]); // in order to log in as this user, we must be activated $this->user->is_activated = true; $this->user->activated_at = now(); $this->user->save(); ``` ... and log-in as that user elsewhere in your unit tests: ```php // register the auth facade $alias = AliasLoader::getInstance(); $alias->alias('Auth', 'RainLab\User\Facades\Auth'); App::singleton('user.auth', function() { return \RainLab\User\Classes\AuthManager::instance(); }); Auth::login($this->user); // we should now be authenticated $this->assertTrue(Auth::check()); ```
References
Add additional online resources to your trick
+ Add reference
Topics
If your trick fits more than one topic select multiple. Select at least one.
Backend
Plugin Development
CMS
Twig
Themes
Deployment
Tags
You can use existing tags or create new ones. Add at least one.
Submit for review
Cancel
We use cookies to measure the performance of this website. Do you want to accept these cookies?
Accept
Decline