|
1 | 1 | <?php |
2 | 2 |
|
3 | | -uses(\Thoughtco\StatamicABTester\Tests\TestCase::class); |
| 3 | +uses(Thoughtco\StatamicABTester\Tests\TestCase::class); |
4 | 4 |
|
5 | 5 | use Statamic\Facades\User; |
6 | 6 | use Thoughtco\StatamicABTester\Facades\Experiment; |
|
43 | 43 | $this->post(cp_route('ab.experiments.actions'), []) |
44 | 44 | ->assertSessionHasErrors(['action']); |
45 | 45 | }); |
| 46 | + |
| 47 | + it('duplicates a manual experiment', function () { |
| 48 | + $experiment = tap(Experiment::make('original') |
| 49 | + ->title('My Experiment') |
| 50 | + ->type('manual') |
| 51 | + ->goals(['goal-1']) |
| 52 | + ->data([ |
| 53 | + 'manual_fields' => [ |
| 54 | + ['handle' => 'control', 'label' => 'Control', 'weight' => 50], |
| 55 | + ['handle' => 'variant_a', 'label' => 'Variant A', 'weight' => 50], |
| 56 | + ], |
| 57 | + ]) |
| 58 | + ->published(true)) |
| 59 | + ->save(); |
| 60 | + |
| 61 | + $this->post(cp_route('ab.experiments.actions'), [ |
| 62 | + 'action' => 'duplicate_experiment', |
| 63 | + 'selections' => [$experiment->id()], |
| 64 | + 'values' => [], |
| 65 | + ])->assertOk(); |
| 66 | + |
| 67 | + expect(Experiment::all()->count())->toBe(2); |
| 68 | + |
| 69 | + $copy = Experiment::all()->reject(fn ($e) => $e->id() === $experiment->id())->first(); |
| 70 | + expect($copy->title())->toBe('Copy of My Experiment'); |
| 71 | + expect($copy->type())->toBe('manual'); |
| 72 | + expect($copy->goals())->toBe(['goal-1']); |
| 73 | + expect($copy->published())->toBeFalse(); |
| 74 | + expect($copy->completedAt())->toBeNull(); |
| 75 | + expect($copy->get('manual_fields'))->toBe($experiment->get('manual_fields')); |
| 76 | + }); |
| 77 | + |
| 78 | + it('duplicates an item experiment preserving experiment_fields and traffic_split', function () { |
| 79 | + $experiment = tap(Experiment::make('item-exp') |
| 80 | + ->title('Item Experiment') |
| 81 | + ->type('item') |
| 82 | + ->goals(['goal-1']) |
| 83 | + ->data([ |
| 84 | + 'item_id' => 'abc-123', |
| 85 | + 'experiment_fields' => ['fields' => ['title'], 'values' => ['title' => 'Variant Title']], |
| 86 | + 'traffic_split' => 30, |
| 87 | + ]) |
| 88 | + ->published(true)) |
| 89 | + ->save(); |
| 90 | + |
| 91 | + $this->post(cp_route('ab.experiments.actions'), [ |
| 92 | + 'action' => 'duplicate_experiment', |
| 93 | + 'selections' => [$experiment->id()], |
| 94 | + 'values' => [], |
| 95 | + ])->assertOk(); |
| 96 | + |
| 97 | + $copy = Experiment::all()->reject(fn ($e) => $e->id() === $experiment->id())->first(); |
| 98 | + expect($copy->title())->toBe('Copy of Item Experiment'); |
| 99 | + expect($copy->type())->toBe('item'); |
| 100 | + expect($copy->get('item_id'))->toBe('abc-123'); |
| 101 | + expect($copy->get('traffic_split'))->toBe(30); |
| 102 | + expect($copy->get('experiment_fields'))->toBe($experiment->get('experiment_fields')); |
| 103 | + expect($copy->published())->toBeFalse(); |
| 104 | + }); |
| 105 | + |
| 106 | + it('redirects to edit page when duplicating a single experiment', function () { |
| 107 | + $experiment = tap(Experiment::make('dup-redirect') |
| 108 | + ->title('Redirect Test') |
| 109 | + ->type('manual') |
| 110 | + ->goals([])) |
| 111 | + ->save(); |
| 112 | + |
| 113 | + $response = $this->post(cp_route('ab.experiments.actions'), [ |
| 114 | + 'action' => 'duplicate_experiment', |
| 115 | + 'selections' => [$experiment->id()], |
| 116 | + 'values' => [], |
| 117 | + ])->assertOk(); |
| 118 | + |
| 119 | + $copyId = Experiment::all() |
| 120 | + ->reject(fn ($e) => $e->id() === $experiment->id()) |
| 121 | + ->first() |
| 122 | + ->id(); |
| 123 | + |
| 124 | + expect($response->json('redirect'))->toBe(cp_route('ab.experiments.edit', $copyId)); |
| 125 | + }); |
| 126 | + |
| 127 | + it('duplicates multiple experiments in bulk without redirecting', function () { |
| 128 | + $exp1 = tap(Experiment::make('bulk-1')->title('Experiment One')->type('manual')->goals([]))->save(); |
| 129 | + $exp2 = tap(Experiment::make('bulk-2')->title('Experiment Two')->type('manual')->goals([]))->save(); |
| 130 | + |
| 131 | + $response = $this->post(cp_route('ab.experiments.actions'), [ |
| 132 | + 'action' => 'duplicate_experiment', |
| 133 | + 'selections' => [$exp1->id(), $exp2->id()], |
| 134 | + 'values' => [], |
| 135 | + ])->assertOk(); |
| 136 | + |
| 137 | + expect(Experiment::all()->count())->toBe(4); |
| 138 | + expect($response->json('redirect'))->toBeFalsy(); |
| 139 | + }); |
| 140 | + |
| 141 | + it('does not copy completed_at when duplicating a completed experiment', function () { |
| 142 | + $experiment = tap(Experiment::make('completed-exp') |
| 143 | + ->title('Completed Experiment') |
| 144 | + ->type('manual') |
| 145 | + ->goals([]) |
| 146 | + ->completedAt(now())) |
| 147 | + ->save(); |
| 148 | + |
| 149 | + $this->post(cp_route('ab.experiments.actions'), [ |
| 150 | + 'action' => 'duplicate_experiment', |
| 151 | + 'selections' => [$experiment->id()], |
| 152 | + 'values' => [], |
| 153 | + ])->assertOk(); |
| 154 | + |
| 155 | + $copy = Experiment::all()->reject(fn ($e) => $e->id() === $experiment->id())->first(); |
| 156 | + expect($copy->completedAt())->toBeNull(); |
| 157 | + expect($copy->published())->toBeFalse(); |
| 158 | + }); |
46 | 159 | }); |
0 commit comments