opnform-host-nginx/api/tests/TestHelpers.php

261 lines
6.9 KiB
PHP
Raw Normal View History

2022-09-20 21:59:52 +02:00
<?php
namespace Tests;
use App\Models\Forms\Form;
use App\Models\User;
2024-02-23 11:54:12 +01:00
use App\Models\Workspace;
2022-09-20 21:59:52 +02:00
use Database\Factories\FormFactory;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
trait TestHelpers
{
/**
* Creates a workspace for a user
*/
public function createUserWorkspace(User $user)
{
if (!$user) {
2024-02-23 11:54:12 +01:00
return null;
}
2022-09-20 21:59:52 +02:00
$workspace = Workspace::create([
'name' => 'Form Testing',
'icon' => '🧪',
]);
$user->workspaces()->sync([
$workspace->id => [
2024-02-23 11:54:12 +01:00
'role' => 'admin',
],
2022-09-20 21:59:52 +02:00
], false);
return $workspace;
}
/**
* Generates a Form instance (not saved)
2024-02-23 11:54:12 +01:00
*
2022-09-20 21:59:52 +02:00
* @return array
*/
public function makeForm(User $user, Workspace $workspace, array $data = [])
{
$dbProperties = [
[
'name' => 'Name',
'type' => 'text',
'hidden' => false,
2024-02-23 11:54:12 +01:00
'required' => false,
2022-09-20 21:59:52 +02:00
],
[
'name' => 'Message',
'type' => 'text',
'hidden' => false,
'required' => false,
2024-02-23 11:54:12 +01:00
'multi_lines' => true,
2022-09-20 21:59:52 +02:00
],
[
'name' => 'Number',
'type' => 'number',
'hidden' => false,
2024-02-23 11:54:12 +01:00
'required' => false,
2022-09-20 21:59:52 +02:00
],
[
'name' => 'Rating',
'type' => 'rating',
'hidden' => false,
'required' => false,
'rating_max_value' => 5
],
[
'name' => 'Scale',
'type' => 'scale',
'hidden' => false,
'required' => false,
'scale_min_value' => 1,
'scale_max_value' => 10,
'scale_step_value' => 1,
],
[
'name' => 'Slider',
'type' => 'slider',
'hidden' => false,
'required' => false,
'slider_min_value' => 1,
'slider_max_value' => 100,
'slider_step_value' => 1,
],
2022-09-20 21:59:52 +02:00
[
'name' => 'Select',
'type' => 'select',
'hidden' => false,
'required' => false,
'allow_creation' => false,
'select' => [
2024-02-23 11:54:12 +01:00
'options' => [['id' => 'First', 'name' => 'First'], ['id' => 'Second', 'name' => 'Second']],
],
2022-09-20 21:59:52 +02:00
],
[
'name' => 'Multi Select',
'type' => 'multi_select',
'hidden' => false,
'required' => false,
'allow_creation' => false,
'multi_select' => [
2024-02-23 11:54:12 +01:00
'options' => [['id' => 'One', 'name' => 'One'], ['id' => 'Two', 'name' => 'Two'], ['id' => 'Three', 'name' => 'Three']],
],
2022-09-20 21:59:52 +02:00
],
[
'name' => 'Date',
'type' => 'date',
'hidden' => false,
2024-02-23 11:54:12 +01:00
'required' => false,
2022-09-20 21:59:52 +02:00
],
[
'name' => 'Checkbox',
'type' => 'checkbox',
'hidden' => false,
2024-02-23 11:54:12 +01:00
'required' => false,
2022-09-20 21:59:52 +02:00
],
[
'name' => 'URL',
'type' => 'url',
'hidden' => false,
2024-02-23 11:54:12 +01:00
'required' => false,
2022-09-20 21:59:52 +02:00
],
[
'name' => 'Email',
'type' => 'email',
'hidden' => false,
2024-02-23 11:54:12 +01:00
'required' => false,
2022-09-20 21:59:52 +02:00
],
[
'name' => 'Phone Number',
'type' => 'phone_number',
'hidden' => false,
2024-02-23 11:54:12 +01:00
'required' => false,
2022-09-20 21:59:52 +02:00
],
[
'name' => 'Files',
'type' => 'files',
'hidden' => false,
'required' => false,
2024-02-23 11:54:12 +01:00
],
2022-09-20 21:59:52 +02:00
];
return Form::factory()
->withProperties(FormFactory::formatProperties($dbProperties))
->forWorkspace($workspace)
->createdBy($user)
2022-11-16 15:57:10 +01:00
->make($data);
2022-09-20 21:59:52 +02:00
}
public function createForm(User $user, Workspace $workspace, array $data = [])
{
$form = $this->makeForm($user, $workspace, $data);
$form->save();
2024-02-23 11:54:12 +01:00
2022-09-20 21:59:52 +02:00
return $form;
}
2022-11-16 15:57:10 +01:00
public function createUser(array $data = [])
2022-09-20 21:59:52 +02:00
{
2022-11-16 15:57:10 +01:00
return \App\Models\User::factory()->create($data);
2022-09-20 21:59:52 +02:00
}
public function createProUser()
{
$user = $this->createUser();
$user->subscriptions()->create([
'type' => 'default',
2022-09-20 21:59:52 +02:00
'stripe_id' => Str::random(),
'stripe_status' => 'active',
'stripe_price' => Str::random(),
'quantity' => 1,
]);
return $user;
}
public function createTrialingUser()
{
$user = $this->createUser();
$user->subscriptions()->create([
'name' => 'default',
'stripe_id' => Str::random(),
'stripe_status' => 'trialing',
'stripe_price' => Str::random(),
'trial_ends_at' => now()->addDays(5),
'quantity' => 1,
]);
return $user;
}
2024-02-23 11:54:12 +01:00
public function actingAsUser(?User $user = null)
2022-09-20 21:59:52 +02:00
{
if ($user == null) {
$user = $this->createUser();
}
2024-01-14 20:52:14 +01:00
$this->postJson('/login', [
2022-09-20 21:59:52 +02:00
'email' => $user->email,
'password' => 'password',
])->assertSuccessful();
return $user;
}
/**
* Creates a user with a Pro subscription
*
* @return User|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|null
*/
2024-02-23 11:54:12 +01:00
public function actingAsProUser(?User $user = null)
2022-09-20 21:59:52 +02:00
{
if ($user == null) {
$user = $this->createProUser();
}
return $this->actingAsUser($user);
}
public function actingAsTrialingUser(User $user = null)
{
if ($user == null) {
$user = $this->createTrialingUser();
}
return $this->actingAsUser($user);
}
2022-09-20 21:59:52 +02:00
public function actingAsGuest()
{
if (Auth::check()) {
Auth::logout();
}
$this->assertGuest();
}
Notification & Integrations refactoring (#346) * Integrations Refactoring - WIP * integrations list & edit - WIP * Fix integration store binding issue * integrations refactor - WIP * Form integration - WIP * Form integration Edit - WIP * Integration Refactor - Slack - WIP * Integration Refactor - Discord - WIP * Integration Refactor - Webhook - WIP * Integration Refactor - Send Submission Confirmation - WIP * Integration Refactor - Backend handler - WIP * Form Integration Status field * Integration Refactor - Backend SubmissionConfirmation - WIP * IntegrationMigration Command * skip confirmation email test case * Small refactoring * FormIntegration status active/inactive * formIntegrationData to integrationData * Rename file name with Integration suffix for integration realted files * Loader on form integrations * WIP * form integration test case * WIP * Added Integration card - working on refactoring * change location for IntegrationCard and update package file * Form Integration Create/Edit in single Modal * Remove integration extra pages * crisp_help_page_slug for integration json * integration logic as collapse * UI improvements * WIP * Trying to debug vue devtools * WIP for integrations * getIntegrationHandler change namespace name * useForm for integration fields + validation structure * Integration Test case & apply validation rules * Apply useform changes to integration other files * validation rules for FormNotificationsMessageActions fields * Zapier integration as coming soon * Update FormCleaner * set default settings for confirmation integration * WIP * Finish validation for all integrations * Updated purify, added integration formatData * Fix testcase * Ran pint; working on integration errors * Handle integration events * command for Delete Old Integration Events * Display Past Events in Modal * on Integration event create with status error send email to form creator * Polish styling * Minor improvements * Finish badge and integration status * Fix tests and add an integration event test * Lint --------- Co-authored-by: Julien Nahum <julien@nahum.net>
2024-03-28 18:14:30 +01:00
public function createFormIntegration($integrationId, $formId, $settings = [])
{
$data = [
'status' => true,
'integration_id' => $integrationId,
'logic' => null,
'settings' => $settings
];
$response = $this->postJson(route('open.forms.integration.create', $formId), $data)
->assertSuccessful()
->assertJson([
'type' => 'success',
'message' => 'Form Integration was created.'
]);
return (object) $response->json('form_integration.data');
}
2022-09-20 21:59:52 +02:00
}