Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature #14782: Possibility to override single translations by database #2213

Merged
merged 16 commits into from
Feb 8, 2022

Conversation

Shnoulle
Copy link
Collaborator

Dev: working solution for user with DB access, no GUI

…tabase

Dev: working solution for user with DB access, no GUI
@c-schmitz
Copy link
Contributor

I am very hesitant to have this as a core feature, because I am afraid that this will diminish contributions to translate.limesurvey.org, because why correct a wrong translation (and contribute back) if you can just quickly fix it in your own instance, only.

@Shnoulle
Copy link
Collaborator Author

See your comment here : https://bugs.limesurvey.org/view.php?id=17163

That would be nice.

seems i made an error in bug report, i update the title

@Shnoulle
Copy link
Collaborator Author

@olleharstedt if it('s OK to be included in core, can you help me on the DB update issue ? I update myself (from 479) : it's OK …

Dev: merge issue with other DB update
@Shnoulle
Copy link
Collaborator Author

@olleharstedt if it('s OK to be included in core, can you help me on the DB update issue ? I update myself (from 479) : it's OK …

OK, seems i have it : 6a87152

@Shnoulle
Copy link
Collaborator Author

Shnoulle commented Jan 20, 2022

I am very hesitant to have this as a core feature, because I am afraid that this will diminish contributions to translate.limesurvey.org, because why correct a wrong translation (and contribute back) if you can just quickly fix it in your own instance, only.

Then , for information :. To have own translation, not translated currently.

With DB (this commit)

  1. Search the english string
  2. Create the string via DB in sourcemessage table
  3. Check the id
  4. Create the translated string in message.

With po (no commit)

  1. Download po file in https://translate.limesurvey.org/projects/
  2. Edit via poedit.
  3. Create ownlocale directory, update
    'basePath' => __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'locale'
  4. Put your updated version in this directory.

@Shnoulle
Copy link
Collaborator Author

Shnoulle commented Jan 20, 2022

The issue in «Compare upgrade and fresh install» is not related to MYISAM against InnoDB ? Since only InnoDB accept foreignKey.

I can not force foreign key and create model, but since we don't use this model …

@olleharstedt an idea ?

@olleharstedt
Copy link
Collaborator

Hm

@olleharstedt
Copy link
Collaborator

olleharstedt commented Jan 20, 2022

I'd like a unit-test of any kind to test this feature, too. If Carsten is willing to merge it, that is.

@Shnoulle
Copy link
Collaborator Author

Shnoulle commented Jan 20, 2022

I'd like a unit-test of any kind to test this feature, too. If Carsten is willing to merge it, that is.

DB update allowed ?

I'm think of a system :

  1. Create a random string
  2. Check translation : return same string ( gT($string) )
  3. Create another random string
  4. Add the DB translation for string1 to string2 (without model)
  5. Check gT($string) again.
  6. Remove the translation

Allowed ?

Else : need to replace LSMessageSource->loadMessages function.

Dev: remove uneeded update of db version
Dev: fixed, not updatabale by config
Dev: fixed, not updatabale by config
…rvey-Shnoulle into develop_translation

# Conflicts:
#	application/core/LSMessageSource.php
@olleharstedt
Copy link
Collaborator

If you mock getDbConnection, you won't need to touch the db at all. I can show you how. Alternatively, you can do anything in the db as long as you restore the previous state. The problem is different tests affecting each other, as we've seen before.

@Shnoulle
Copy link
Collaborator Author

function is now this :

    public function getDbConnection()
    {
        return App()->getDb();
    }

I need to mock->createCommand() and queryAll() right. It's already exist in another test ? More quick to copy/paste ;)

@olleharstedt
Copy link
Collaborator

I need to mock->createCommand() and queryAll() right. It's already exist in another test ? More quick to copy/paste ;)

Something like that. But what you need to mock differs depending on which function (unit) you test. Let me check...

@olleharstedt
Copy link
Collaborator

olleharstedt commented Jan 21, 2022

Hm, maybe it's easier to mock getMessagesFromDb() instead.

And then test getMessagesFromDb() with real db data (which is deleted after test is run, so called fixture).

@olleharstedt
Copy link
Collaborator

Mock docs for PHPUnit: https://phpunit.readthedocs.io/en/9.5/test-doubles.html?highlight=mock

I don't think we're using version 9.5 tho, you'd have to double check.

@olleharstedt
Copy link
Collaborator

public function testLsMessageSource()
{
    $messageSource = new LSMessageSource();
    $messageSource->basePath = YII_PATH . '/../locale/';
    $ref = new ReflectionClass('LSMessageSource');
    $met = $ref->getMethod('loadMessages');
    $met->setAccessible(true);
    $messages = $met->invokeArgs($messageSource, ['', 'en']);
    $this->assertNotEmpty($messages);
}

One example of calling loadMessages (even tho it's protected) and test the result.

@Shnoulle
Copy link
Collaborator Author

Shnoulle commented Feb 4, 2022

One example of calling loadMessages (even tho it's protected) and test the result.

Interest ? Testing a 2 functions via phpunit …

I can add if you want , but didn't see the interest.

The current test check gT function.

@olleharstedt
Copy link
Collaborator

olleharstedt commented Feb 4, 2022

Well, optimally, you're supposed to test each possible path in the function - with cache, without cache, with db, without db, etc. That will be hard only using gT(). Just using gT(), you might not get 100% or 50% coverage.

You're also supposed to do negative tests - testing that the function fails correctly.

@olleharstedt
Copy link
Collaborator

Hm, on another note, I'm not able to repeat your test multiple times locally. It works once, but then I get this:

1) ls\tests\LSMessageSourceTest::testUpdatedString
Message translation by DB is broken
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'translatedmessageP8YlHKPocMV98wFEZeolVgWhTYm5FbadrKkzYM~7UX'
+'sourcemessageCgfg5jHVZ5MhS_F_pQ4D7LFi~QRv7SY0s9gL9dqjmn'

Related to random string?

@Shnoulle
Copy link
Collaborator Author

Shnoulle commented Feb 4, 2022

Well, optimally, you're supposed to test each possible path in the function - with cache, without cache, with db, without db, etc. That will be hard only using gT(). Just using gT(), you might not get 100% or 50% coverage.

And with your test : what is the coverage ? What do you test ?

I can not create test for all previous feature each time : it's not part of the coverage… i have to test only the new feature.

What is the coverage here : «Possibility to override single translation by db» OK to add caching (translation not updated with DB update only), but don't understand witout db …

@Shnoulle
Copy link
Collaborator Author

Shnoulle commented Feb 4, 2022

Hm, on another note, I'm not able to repeat your test multiple times locally. It works once, but then I get this:

1) ls\tests\LSMessageSourceTest::testUpdatedString
Message translation by DB is broken
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'translatedmessageP8YlHKPocMV98wFEZeolVgWhTYm5FbadrKkzYM~7UX'
+'sourcemessageCgfg5jHVZ5MhS_F_pQ4D7LFi~QRv7SY0s9gL9dqjmn'

Related to random string?

Related to caching … cache for 1 hour by default.

With caching : we don't get the new translation : we return the cache.

@olleharstedt
Copy link
Collaborator

Related to caching … cache for 1 hour by default.

OK, so I need to disable caching? You see one benefit at once with my approach: just set $messageSource->cachingDuration = 0 in the test code. :)

@Shnoulle
Copy link
Collaborator Author

Shnoulle commented Feb 4, 2022

Related to caching … cache for 1 hour by default.

OK, so I need to disable caching? You see one benefit at once with my approach: just set $messageSource->cachingDuration = 0 in the test code. :)

I already think we need to check if caching work .

Best seems to

  1. Reset caching at start
  2. Create the new translation in DB
  3. Call existing po DB string : (Submit ? Yes ? anything can be updated at a time
  4. Call a random string : must return new translation
  5. Update the random string in DB : must retuen previous one

The lack is update core string , then if i can add a translation for Submit : it's OK.

But in this condition : if user have updated his own language : didn't work   …

I create all of this

@Shnoulle Shnoulle marked this pull request as draft February 4, 2022 11:18
@olleharstedt
Copy link
Collaborator

I already think we need to check if caching work .

Maaaybe. Caching is part of Yii, and we should assume it's working (Yii has its own test suite).

@Shnoulle
Copy link
Collaborator Author

Shnoulle commented Feb 4, 2022

Well, optimally, you're supposed to test each possible path in the function - with cache, without cache, with db, without db, etc. That will be hard only using gT(). Just using gT(), you might not get 100% or 50% coverage.

You're also supposed to do negative tests - testing that the function fails correctly.

I already think we need to check if caching work .

Maaaybe. Caching is part of Yii, and we should assume it's working (Yii has its own test suite).

????

@olleharstedt
Copy link
Collaborator

Oh, I'm contradicting myself. 🤦

@Shnoulle
Copy link
Collaborator Author

Shnoulle commented Feb 4, 2022

we should assume

Like : we should assume po/mo file work (or not ?)

The «we should assume» the default way to get translation use LSMessageSource can not be assumed.

gT whole system can be updated too easily …

@olleharstedt
Copy link
Collaborator

Like : we should assume po/mo file work (or not ?)

I think, assuming po/mo files are correct, is the same as mocking the access to mo/po files in the unit test. Which is one way to do it. But it's OK to just access those files, too.

@Shnoulle
Copy link
Collaborator Author

Shnoulle commented Feb 4, 2022

Like : we should assume po/mo file work (or not ?)

I think, assuming po/mo files are correct, is the same as mocking the access to mo/po files in the unit test. Which is one way to do it. But it's OK to just access those files, too.

Again : mocking a 2 function call in same class … i don't really understand the point here …

I don't understand what you want …

@Shnoulle Shnoulle marked this pull request as ready for review February 4, 2022 14:06
@Shnoulle Shnoulle marked this pull request as draft February 5, 2022 18:39
@Shnoulle
Copy link
Collaborator Author

Shnoulle commented Feb 6, 2022

One example of calling loadMessages (even tho it's protected) and test the result.

Any idea to mock loadMessagesFromDb here ? Because without this : i didn't test this feature …

@Shnoulle Shnoulle marked this pull request as ready for review February 6, 2022 17:22
@Shnoulle
Copy link
Collaborator Author

Shnoulle commented Feb 6, 2022

Check if po translation work + check if file cache (by default) work + DB new source + DB replace source.

I don't see what i can do now …

@olleharstedt
Copy link
Collaborator

Great! Is it now possible to run the test multiple times locally?

@Shnoulle
Copy link
Collaborator Author

Shnoulle commented Feb 7, 2022

Great! Is it now possible to run the test multiple times locally?

I add a resetCache function.
Default behaviour is to reset DB cache only :

\Yii::app()->db->schema->refresh();

I add https://github.com/LimeSurvey/LimeSurvey/pull/2213/files#diff-3cc131b4d608fd992d775790fbc8c5f5b04a49347a85a54390af3291c6b504e0R44
(PS : maybe we need an API function for this)

@olleharstedt
Copy link
Collaborator

OK, gonna test locally again...

@Shnoulle
Copy link
Collaborator Author

Shnoulle commented Feb 7, 2022

LSMessage Source (ls\tests\LSMessageSource)
✔ Updated string

@olleharstedt
Copy link
Collaborator

Works multiple times for me locally 👍 🌟

@c-schmitz c-schmitz merged commit 1ed3005 into LimeSurvey:develop Feb 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants