Skip to content

Commit

Permalink
Merge pull request #603 from alicolville/if-statement-tweaks
Browse files Browse the repository at this point in the history
If statement tweaks
  • Loading branch information
alicolville authored Oct 19, 2024
2 parents eb83e05 + f1f76e7 commit 02b9a61
Show file tree
Hide file tree
Showing 129 changed files with 932 additions and 26 deletions.
Binary file modified .DS_Store
Binary file not shown.
10 changes: 5 additions & 5 deletions .auth/user.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cookies": [
{
"name": "wordpress_86a9106ae65537651a8e456835b316ab",
"value": "playwright%7C1727959399%7Cgt2bI1XqzzarJ6XhXma1A1TFjnwzeFTWMnHLC7CESuB%7C628e4f6322e205ecf4250e069a16a2e87fa979bda47a8f8b7176d7a5eeeb064b",
"value": "playwright%7C1727967075%7CpdUc4INEgsjMxUk1ak2vP4QYn7tj2jAm29ZL86F2mjT%7Cbdebf73609d169cbd5a1bbdc90a1e77751d9cbe79140fb586cce22bcad585bb1",
"domain": "localhost",
"path": "/wp-content/plugins",
"expires": -1,
Expand All @@ -12,7 +12,7 @@
},
{
"name": "wordpress_86a9106ae65537651a8e456835b316ab",
"value": "playwright%7C1727959399%7Cgt2bI1XqzzarJ6XhXma1A1TFjnwzeFTWMnHLC7CESuB%7C628e4f6322e205ecf4250e069a16a2e87fa979bda47a8f8b7176d7a5eeeb064b",
"value": "playwright%7C1727967075%7CpdUc4INEgsjMxUk1ak2vP4QYn7tj2jAm29ZL86F2mjT%7Cbdebf73609d169cbd5a1bbdc90a1e77751d9cbe79140fb586cce22bcad585bb1",
"domain": "localhost",
"path": "/wp-admin",
"expires": -1,
Expand All @@ -32,7 +32,7 @@
},
{
"name": "wordpress_logged_in_86a9106ae65537651a8e456835b316ab",
"value": "playwright%7C1727959399%7Cgt2bI1XqzzarJ6XhXma1A1TFjnwzeFTWMnHLC7CESuB%7C538e8e3feb82bde097726694a13406d1db167c86694075aab2955f4f5070051a",
"value": "playwright%7C1727967075%7CpdUc4INEgsjMxUk1ak2vP4QYn7tj2jAm29ZL86F2mjT%7Cd0900d38722f628d52d219d7563705322c8568f634a0e770915716568b5462b0",
"domain": "localhost",
"path": "/",
"expires": -1,
Expand All @@ -42,10 +42,10 @@
},
{
"name": "wp-settings-time-8",
"value": "1727786599",
"value": "1727794275",
"domain": "localhost",
"path": "/",
"expires": 1759322599.866692,
"expires": 1759330275.24052,
"httpOnly": false,
"secure": false,
"sameSite": "Lax"
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ assets/.DS_Store
.DS_Store
node_modules/.bin/playwright
playwright-report/
playwright/.auth/user.json
.auth/user.json
46 changes: 43 additions & 3 deletions docs/shortcodes/wt-if.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
In some cases, you may only wish to display content if the user has completed certain WT fields. Let's say for example, you have a page where you wish to display a chart and some text introductory text, there is no point displaying the chart if the user has not entered their weight. The shortcode [wt-if] allows you some basic logic to specify “if exists” or “if not exists” type logic. So using the example above, you could have something like:

[wt-if field="weight" operator="exists]
[wt-if field="weight" operator="exists"]
Some introductory text here explaining the chart.
[wt-chart]
[/wt-if]

We can also further expand this with an [else] clause. If the above condition is not met, we can specify what should be display instead e.g.

[wt-if field="weight" operator="exists]
[wt-if field="weight" operator="exists"]
Some introductory text here explaining the chart.
[wt-chart]
[else]
Expand All @@ -30,6 +30,46 @@ You may wish to create an AND condition where multiple fields must exist or not

At the moment this shortcode is in it's infancy, so [please get in touch]({{ site.baseurl }}/contact.html) with any suggestions. Below you can see the fields and operators it supports and further examples of its usage [can be found on the [wt-if] gist](https://gist.github.com/alicolville/d33fbdabc628c92e4e40b7f08b343fe7).

**Comparison IF statements**

In some cases, you may wish to check if a field is greater than, less than or equal to a given number. For this, the following "operator" values exist:

```
'equals', 'greater-than','greater-than-or-equal-to', 'less-than' or 'less-than-or-equal-to'.
```

> Note: Weight comparisons will be made in either kilograms (kg) or pounds (lbs), depending on your default weight unit.
Compaison IF statements can only be used with the following fields:

| Field | Description |
|--|--|
|difference-from-previous|Difference between latest weight and previous weight
|difference-from-start|Difference between latest weight and start weight
|no-days|Number of days a user has been entering data for (latest date - start date)
|no-entries|Number of weight entries a user has made
|previous-weight|Previous weight|
|target|User's target|
|Weight|User's latest weight|

> Note: With comparisons, you can only specify one field.
Below are some examples:

```
[wt-if field="no-entries" operator="less-than-or-equal-to" value=5]
The user has made 5 or less entries.
[/wt-if]
[wt-if field="difference-from-start" operator="greater-than" value=20]
Well done, you have lost more than 20Kg!!
[/wt-if]
[wt-if field="target" operator="less-than" value=4]
You've entered quite a low target, why not challenge yourself and set it just a little higher?
[/wt-if]
```

**Nesting [wt-if] statements**

There maybe instances where you wish to nest IF statements. For example you may want to ensure a person is logged in before checking if their weight and height. To do this, you can use the additional shortcodes to nest [wt-if-1], [wt-if-2] and [wt-if-3]. Each corresponds to the level of nesting. Below is a simple example:
Expand Down Expand Up @@ -61,7 +101,7 @@ The shortcode supports the following arguments:
| Argument | Description | Options | Example |
|--|--|--|--|
|field|Allows you to specify which field should be examined.|weight (default), previous-weight, is-logged-in, challenges-opted-in, target, bmr, height, aim, gender, photo, activity-level or dob.|[wt-if field="dob" operator="not-exists"]Please enter your Date of Birth on the settings page.[/wt-if]
|operator|Allows you to state whether field has been populated (exists) or not (not-exists).|exists (default) or not-exists|[wt-if operator="exists" field="weight"]Thank you for adding a weight entry[else]Please add a weight entry[/wt-if]
|operator|Allows you to state whether field has been populated (exists) or not (not-exists).|exists (default), exists, equals, greater-than,greater-than-or-equal-to, less-than or less-than-or-equal-to|[wt-if operator="exists" field="weight"]Thank you for adding a weight entry[else]Please add a weight entry[/wt-if]
|strip-p-br|Specifies whether to remove <p> and <br> tags added by WordPress|true or false (default)|[wt-if strip-p-br="true"]Something[/wt-if]
|user-id|By default, the shortcode will determine the result for the current user. If you wish to determine the result based on another user, use the following argument.|Numeric| [wt-if user-id="1" field="weight"]Something[/wt-if]

Expand Down
49 changes: 49 additions & 0 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,55 @@ function ws_ls_entry_get_oldest_kg( $user_id ) {
return ws_ls_entry_get_oldest( [ 'user-id' => $user_id, 'meta' => false, 'kg-only' => true ] );
}

/**
* Return the percentage or numeric difference between the latest and previous weight.
*
* Note: Slimmed down version of ws_ls_shortcode_difference_in_weight_previous_latest() - should probably look to refactor
*
* @param $user_id
* @param $percentage
* @return float|null
*/
function ws_ls_entry_difference_between_latest_and_previous( $user_id = NULL, $percentage = false ) {

$user_id = ( NULL === $user_id ) ? get_current_user_id() : $user_id;

$cache_key = 'entry-diff-prev-latest-' . (int) $user_id;

if ( $cache = ws_ls_cache_user_get( $user_id, $cache_key ) ) {
return $cache;
}

$latest_entry = ws_ls_entry_get_latest( [ 'user-id' => $user_id ] );

if ( true === empty( $latest_entry[ 'kg' ] ) ) {
return '';
}

$previous_entry = ws_ls_entry_get_previous( [ 'user-id' => $user_id ] );

if( true === empty( $previous_entry ) ) {
return NULL;
}

if ( $previous_entry[ 'id' ] === $latest_entry[ 'id' ] ) {
return NULL;
}

if ( true === $percentage ) {

$output = ws_ls_calculate_percentage_difference( $previous_entry[ 'kg' ], $latest_entry[ 'kg' ] );

if ( true === empty( $output[ 'percentage' ] ) ) {
return NULL;
}

return ( true === $output[ 'increase' ] ) ? $output[ 'percentage' ] : -$output[ 'percentage' ];
}

return $latest_entry[ 'kg' ] - $previous_entry[ 'kg' ];
}

/**
* Fetch the start date for the given user
*
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions playwright/playwright.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ export default defineConfig({
reporter: "html",
testDir: "tests",
timeout: 5000,
workers: 1 // Not ideal, but the data being manipulated by several tests at once was causing sporadic issues.
});
1 change: 0 additions & 1 deletion playwright/tests/shortcode.wt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ test.describe( 'WT Shortcode', () => {

test.describe.configure( { mode: 'serial' } );


test('exist on page', async ({ page }) => {
await page.goto('http://localhost/weight-tracker/');

Expand Down
58 changes: 58 additions & 0 deletions playwright/tests/weight-tracker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import type { Page, Locator } from '@playwright/test';
import { expect } from '@playwright/test';
/**
* This class is used by other tests to simulate Weight Tracker [wt] actions
*/

export class WeightTracker {

constructor(public readonly page: Page) {
}

async goto() {
await this.page.goto('http://localhost/weight-tracker/');
}

async weight_set_defaults(){
await this.goto();
await this.weight_clear_all();
await this.weight_add( '01/01/2019', '200' );
}

async weight_add( date, weight){
await this.goto();
await this.page.getByTestId('wt-tab-add-edit').click();
await this.page.getByTestId('we-ls-date').fill(date);
await this.page.getByTestId('wt-tab-add-edit').click();
await this.page.getByTestId('ws-form-weight').fill(weight);
await this.page.getByRole('button', { name: 'Save Entry' }).click();
}

async weight_clear_all(){
await this.goto();
await this.page.getByTestId('wt-tab-settings').click();
await this.page.getByLabel('The button below allows you').selectOption('yes');
await this.page.getByRole('button', { name: 'Delete' }).click();
await expect(this.page.locator('#wp--skip-link--target')).toContainText('Your data has successfully been deleted.');
}

async target_set(target){
await this.goto();
await this.page.getByRole('link', { name: 'Adjust' }).click();
await this.page.getByTestId('ws-form-target').click();
await this.page.getByTestId('ws-form-target').fill( target);
await this.page.getByRole('button', { name: 'Set Target' }).click();
}

async target_clear(){
await this.goto();
await this.page.getByTestId('wt-tab-settings').click();

const visible = await this.page.getByRole('button', { name: 'Clear Target' }).isVisible();

if (visible) {
this.page.on('dialog', dialog => dialog.accept());
await this.page.getByRole('button', { name: 'Clear Target' }).click();
}
}
}
76 changes: 76 additions & 0 deletions playwright/tests/wt-if-difference-from-start.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { test as base, expect } from '@playwright/test';
import { WeightTracker } from './weight-tracker';


const test = base.extend<{ weightTracker: WeightTracker }>({
weightTracker: async ({ page }, use) => {

// Clear all weight entries and add a start weight.
const weightTracker = new WeightTracker(page);

await weightTracker.weight_set_defaults();
await use(weightTracker);
},
});

test.describe.configure( { mode: 'serial' } );

test('wt-if difference from start', async ({ weightTracker, page }) => {

await page.goto('http://localhost/if-statements/difference-from-start/');
await expect(page.locator('.greater-than')).toContainText('greater than: no');
await expect(page.locator('.greater-than-or-equal-to')).toContainText('greater than or equal to: no');
await expect(page.locator('.less-than')).toContainText('less than: no');
await expect(page.locator('.less-than-or-equal-to')).toContainText('less than or equal to: no');
await expect(page.locator('.equals')).toContainText('equals: no');

await weightTracker.weight_add( '11/01/2024', '213' );

await page.goto('http://localhost/if-statements/difference-from-start/');
await expect(page.locator('.greater-than')).toContainText('greater than: no');
await expect(page.locator('.greater-than-or-equal-to')).toContainText('greater than or equal to: no');
await expect(page.locator('.less-than')).toContainText('less than: yes');
await expect(page.locator('.less-than-or-equal-to')).toContainText('less than or equal to: yes');
await expect(page.locator('.equals')).toContainText('equals: no');

await weightTracker.weight_add( '11/01/2024', '214' );

await page.goto('http://localhost/if-statements/difference-from-start/');
await expect(page.locator('.less-than-or-equal-to')).toContainText('less than or equal to: no');

await weightTracker.weight_add( '11/02/2024', '250' );

await page.goto('http://localhost/if-statements/difference-from-start/');
await expect(page.locator('.greater-than')).toContainText('greater than: no');
await expect(page.locator('.greater-than-or-equal-to')).toContainText('greater than or equal to: no');
await expect(page.locator('.less-than')).toContainText('less than: no');
await expect(page.locator('.less-than-or-equal-to')).toContainText('less than or equal to: no');
await expect(page.locator('.equals')).toContainText('equals: yes');

await weightTracker.weight_add( '11/03/2024', '280' );

await page.goto('http://localhost/if-statements/difference-from-start/');
await expect(page.locator('.greater-than')).toContainText('greater than: no');

await weightTracker.weight_add( '11/04/2024', '281' );

await page.goto('http://localhost/if-statements/difference-from-start/');
await expect(page.locator('.greater-than')).toContainText('greater than: yes');

await weightTracker.weight_add( '11/04/2024', '290' );

await page.goto('http://localhost/if-statements/difference-from-start/');
await expect(page.locator('.greater-than')).toContainText('greater than: yes');
await expect(page.locator('.greater-than-or-equal-to')).toContainText('greater than or equal to: yes');
await expect(page.locator('.less-than')).toContainText('less than: no');
await expect(page.locator('.less-than-or-equal-to')).toContainText('less than or equal to: no');
await expect(page.locator('.equals')).toContainText('equals: no');

await weightTracker.weight_add( '11/04/2024', '289' );

await page.goto('http://localhost/if-statements/difference-from-start/');
await expect(page.locator('.greater-than-or-equal-to')).toContainText('greater than or equal to: no');

await weightTracker.weight_set_defaults();

});
59 changes: 59 additions & 0 deletions playwright/tests/wt-if-number-of-days.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { test as base, expect } from '@playwright/test';
import { WeightTracker } from './weight-tracker';


const test = base.extend<{ weightTracker: WeightTracker }>({
weightTracker: async ({ page }, use) => {

// Clear all weight entries and add a start weight.
const weightTracker = new WeightTracker(page);

await weightTracker.weight_set_defaults();
await use(weightTracker);
},
});

test('basic tests', async ({ weightTracker, page }) => {

await page.goto('http://localhost/if-statements/number-of-days/');
await expect(page.locator('.greater-than')).toContainText('greater than: no');
await expect(page.locator('.greater-than-or-equal-to')).toContainText('greater than or equal to: no');
await expect(page.locator('.less-than')).toContainText('less than: yes');
await expect(page.locator('.less-than-or-equal-to')).toContainText('less than or equal to: yes');
await expect(page.locator('.equals')).toContainText('equals: no');

await weightTracker.weight_add( '11/01/2019', '300' );

await page.goto('http://localhost/if-statements/number-of-days/');
await expect(page.locator('.greater-than')).toContainText('greater than: no');
await expect(page.locator('.greater-than-or-equal-to')).toContainText('greater than or equal to: no');
await expect(page.locator('.less-than')).toContainText('less than: yes');
await expect(page.locator('.less-than-or-equal-to')).toContainText('less than or equal to: yes');
await expect(page.locator('.equals')).toContainText('equals: yes');

await weightTracker.weight_add( '16/01/2019', '200' );

await page.goto('http://localhost/if-statements/number-of-days/');
await expect(page.locator('.greater-than')).toContainText('greater than: yes');
await expect(page.locator('.greater-than-or-equal-to')).toContainText('greater than or equal to: yes');
await expect(page.locator('.less-than')).toContainText('less than: yes');
await expect(page.locator('.less-than-or-equal-to')).toContainText('less than or equal to: yes');
await expect(page.locator('.equals')).toContainText('equals: no');

await weightTracker.weight_add( '26/01/2019', '200' );

await page.goto('http://localhost/if-statements/number-of-days/');
await expect(page.locator('.greater-than')).toContainText('greater than: yes');
await expect(page.locator('.greater-than-or-equal-to')).toContainText('greater than or equal to: yes');
await expect(page.locator('.less-than')).toContainText('less than: no');
await expect(page.locator('.less-than-or-equal-to')).toContainText('less than or equal to: yes');
await expect(page.locator('.equals')).toContainText('equals: no');

await weightTracker.weight_add( '26/02/2019', '200' );

await page.goto('http://localhost/if-statements/number-of-days/');
await expect(page.locator('.less-than-or-equal-to')).toContainText('less than or equal to: no');

await weightTracker.weight_set_defaults();

});
Loading

0 comments on commit 02b9a61

Please sign in to comment.